473,473 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

classmethods, staticmethods, and decorators


Probably the best-loved features of Python are the shortcuts. The fact
that you don't have to explicitly declare very much and that the
language supports clear, shorthand syntax for frequently used operations...

x = [1,2,3]

y = r[2:9]

def __foo():

That's the good stuff of Python.

I believe that identifying staticmethods and classmethods is an
operation that is (or will be) frequently used as compared to the other
decorators. So it would be great if there was an obvious, shorthand way
for doing that as well.

Even better (IMO) would be a solution that didn't really add any new
keywords to the language, but instead simply formalized widely-used
coding conventions, as was done to denote code block boundaries.

The use of indentation to identify code blocks is a wonderful example of
Python shorthand. It works (some even call it 'genius') because it
leverages a coding convention shared by a majority of programmers.

A week or so ago on this forum, Stefan Eischet suggested that the type
(static|class|instance) of a method should be inferred through
examination of its first parameter. I agree with his assessment.

The vast majority of instance methods I've seen all use 'self' as the
first parameter. Likewise, most class methods use 'cls' or 'klass' as
their first parameter. If we exploit these conventions, we end up with
a simple, clear, obvious mechanism for denoting (this aspect of) a
method's type.

class Foo(Object):
def m1(self, a, b): # this is an instance method of Foo
pass

def m2(cls, a, b): # this is a class method of Foo
pass

def m3(a, b): # this is a static method of Foo
pass

And for backwards compatibility, a special Object (capital 'O') class
could work this magic so that old code didn't break.

I know that this is odd. But then so are most of the great things about
Python.

Paul
Jul 18 '05 #1
0 1365

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
9
by: jfj | last post by:
Hi. Suppose this: ######################## def foo (x): print x f = classmethod (foo)
12
by: Andrew Jaffe | last post by:
Hi, I have a class with various class-level variables which are used to store global state information for all instances of a class. These are set by a classmethod as in the following (in...
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
2
by: Steven D'Aprano | last post by:
There's some subtle behaviour going on here that I don't really follow. Class methods apparently aren't classmethods. .... def method(self, *args): .... return self, args .......
7
by: Ivan Illarionov | last post by:
After re-reading "Python is not Java" I finally came to conclusion that classmethods in Python are a very Bad Thing. I can't see any use-case of them that couldn't be re-written more clearly...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.