Possible PEP: Improve classmethod/staticmethod syntax

Aahz aahz at pythoncraft.com
Wed Jun 4 05:43:37 CEST 2003


[BTW, please make sure to attribute quotes.  I've restored the correct
attributions.]

In article <PNdDa.824522$OV.780836 at rwcrnsc54>, Bryan <belred1 at yahoo.com> wrote:
>Aahz wrote:
>>In article <20030603164521903-0400 at braeburn.themorgue.org>,
>>Kevin Smith  <Kevin.Smith at sas.com> wrote:
>>>
>>>The current way of declaring a method to be a class method or a static
>>>method leaves much to be desired.  While it is flexible in the sense
>>>that new types of method modifiers can be created without any
>>>syntactical backwards incompatibilities, it just isn't that user-
>>>friendly (and, in turn, not Pythonic).
>>
>> No argument; Guido considers the current method experimental.  The
>> current front-runner for fixing this is to add function decorators:
>>
>>     def myMethod(args) [staticmethod]:
>>         blah, blah, blah
>>
>> staticmethod would be a callable object that would return the
>> replacement function object.  This would allow chained decorators.
>
> i'm sorry, but i'm not following you. can you please explain to me
>what you mean by staticmethod being a replacement function object? so,
>staticmethod isn't a keyword here? i must be missing something here,
>because this seems weird to me.

staticmethod is not a replacement function object, it *returns* a
replacement function object for myMethod.  That is, the object bound to
myMethod (*not* "myMethod()") is passed to staticmethod, and staticmethod
returns an object that is re-bound to myMethod.  You are correct that
staticmethod is not a keyword, any more than "str", "list", "len", or
"myMethod" are keywords -- that is in fact the whole point of this
proposal, because it requires no new keywords and a minimal change to
the Python parser.

A key advantage is that adding Design By Contract to Python becomes much
easier this way.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra




More information about the Python-list mailing list