PEP 318: Can't we all just get along?

Jeff Shannon jeff at
Fri Aug 20 21:03:54 CEST 2004

Neil Zanella wrote:

>To summarize, here are the two reasons to use staticmethod:
>1. You want to group a bunch of functions together. However, as it happens
>   such functions are not useful outside of a given class, so you use
>   staticmethod to place them where they belong. Perhaps you do not
>   have enough such functions to warrant their placement inside a
>   module of its own.
>2. You need to use inheritance and polymorphism but you don't need instances,
>   or perhaps you simply need instance independent polymorphism at the class
>   level.

It seems to me that the most common case for staticmethod is when you 
have a function which does not rely directly on the attributes of an 
existing class/instance, but which *conceptually* belongs with that 
class.  For example, in wxPython, the wxImage class needs to have 
handlers for various filetypes installed before one can create wxImage 
instances from files of that type.  You can't use a normal method, 
obviously, because you need to install these handlers before you have an 
instance to call a method on.  You can implement them as a totally 
independent function (and wxPython used to do this), using a naming 
convention to indicate the relatedness.  But really, the best way to do 
this is as a staticmethod on the wxImage class.  It can be called before 
any instances are created, and it paves the way for creating such instances.

(Both of your examples seem set on the idea of classes which contain 
*only* staticmethods; I would submit that this is an extremely unusual 
case, though perhaps not without some utility.)

Jeff Shannon
Credit International

More information about the Python-list mailing list