why is there no class (static) methods in Python ?

James_Althoff at i2.com James_Althoff at i2.com
Mon Jun 18 23:19:29 CEST 2001


Carel Fellinger wrote:
>Alex Martelli <aleaxit at yahoo.com> wrote:
>> "Richard Gruet" <rgruet at intraware.com> wrote in message
>> news:3B2D114E.7B39D954 at intraware.com...
>...
>>> class C:
>>>     def staticFoo(x): print x
>>>
>>> C.staticFoo(1)
>
>> http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52304
>
>according to the faq there is an even shorter spelling of this idiom:
>
>class Callable:
>    def __init__(self, callable):
>       self.__call__ = callable
>
>this works as the magic method transformation only applies to
>class-functions, not to instance-functions.
>
>To me this spelling of the idiom is a tad more readable, besides it
>being more efficient as it saves one method call.

Note that this "faq/recipe/trick" (ugly and awkward as it might be) --
though doing a reasonable job of emulating C++ and Java-style "static"
class methods -- does not provide "complete-functionality" class methods (a
la Smalltalk) in that it is missing the "override" mechanism (because there
is no "self" -- meaning the class object -- argument in these "static"
class methods).  IOW, if one of these "static" class methods calls another
"static" class method and you want to inherit/reuse the first and override
the second, you have a problem (the same problem you have if you use a
module function as a "poor man's" class method).

Jim






More information about the Python-list mailing list