staticmethod

Erik Max Francis max at alcyone.com
Wed Feb 26 01:36:53 CET 2003


Jeremy Yallop wrote:

> I find staticmethod()'s behaviour a bit surprising.  In particular:
> 
> Why does staticmethod() `work' on uncallable objects?
> 
>   >>> staticmethod('huh?')
>   <staticmethod object at 0x8152eb8>
> 
> I'd expect this to raise an exception.

staticmethod just creates a wrapper.  It doesn't check whether the
object in question is callable since Python is dynamic and, when passed
arbitrary objects, it's not trivial to tell whether the object is
callable unless you actually try calling it (and see if it fails).  In
other words, Python gives you the benefit of the doubt and supposes you
know what you're doing.

> To be clear, I know the `proper' way to call staticmethod().  It's the
> behaviour when it's used in a way that differs from the standard
> example that's puzzling me.

The short answer is that staticmethod (and its friend, classmethod)
needs to be in the right scope when invoked to work properly.  Using it
outside that scope neans that it won't work right.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The more one is hated, I find, the happier one is.
\__/ Louis-Ferdinand Celine
    Bosskey.net / http://www.bosskey.net/
 A personal guide to online multiplayer first person shooters.




More information about the Python-list mailing list