473,587 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ 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|i nstance) 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 1382

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

Similar topics

17
1762
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. Previously, when looking at: some_python(code) and_some_more = stuff there was no need to look at the the first line in order to know what
4
1480
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 cleanliness of python.
2
1700
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 to accept it would be to pacify the supporters of the proposal, and that just isn't a good enough reason in language design. However, it got pretty darn close! I'm impressed with how the community managed to pull together and face the enormous...
0
2337
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 Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
9
4599
by: jfj | last post by:
Hi. Suppose this: ######################## def foo (x): print x f = classmethod (foo)
12
1781
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 reality the setcvar method is more complicated than this!): class sup(object): cvar1 = None cvar2 = None
2
1945
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 file when I import it? Or perhaps anyway to find the decorators by loading the file by other methods (with out simply parsing it by hand). Basically what I'm looking for is a way to, given a python file, look through that file and find all the...
2
1211
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 .... @classmethod .... def cmethod(cls, *args): .... return cls, args ....
7
1729
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 with methods of metaclass or plain functions. They have the following issues: 1. You mix instance-level and class-level functionality in one place making your code a mess. 2. They are slower than metaclass methods or plain functions.
0
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.