Make staticmethod objects callable?

Terry Reedy tjreedy at udel.edu
Wed Mar 1 15:16:16 EST 2006


"Steven D'Aprano" <steve at REMOVEMEcyber.com.au> wrote in message 
news:44055BE0.8030409 at REMOVEMEcyber.com.au...
> >>> class Parrot:
> ...     def speak():
> ...             return "dead parrot"
> ...     speak = staticmethod(speak)
> ...     def playdead():
> ...             return "still dead"
> ...
> >>> type(Parrot.speak)
> <type 'function'>
> >>> type(Parrot.playdead)
> <type 'instancemethod'>
>
> So, based on this evidence, staticmethod() converts an
> instance method into an ordinary function. Parrot.speak
> certainly behaves like an ordinary function.

Actually, staticmethod() prevents an ordinary function from being converted 
to (wrapped as) a method upon access via the class.

>>> class C(object):
 def f(s): pass

>>> type(C.__dict__['f'])
<type 'function'>
>>> type(C.f)
<type 'instancemethod'>

As to the general topic: my impression is that staticmethod was a rather 
optional addon with limited usage and that beginners hardly need to know 
about it.

Terry Jan Reedy






More information about the Python-list mailing list