C Gillespie wrote:
> Does anyone know of any examples on how (& where) to use staticmethods and
> classmethods?

My personal experience is that I almost *never* want a staticmethod. 
The things that I would have written as a staticmethod in Java I simply 
write as a module-level function in Python.

I do occasionally used classmethods though to build alternate 
constructors.  I recently had a class with a constructor that looked like:

class C(object):
     def __init__(self, *args):
         ...

I already had code working with this constructor, but I needed to add an 
optional 'index' parameter.  I found the clearest way to do this was 
something like:

class C(object):
     def __init__(self, *args):
         ...
     @classmethod
     def indexed(cls, index, *args):
         ...
         obj = cls(*args)
         obj.index = index
         ...
         return obj

Then the classes that needed the 'index' parameter simply use 
C.indexed() as the constructor instead of C().

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to