staticmethod and classmethod

John Roth newsgroups at jhrothjr.com
Tue May 24 13:49:08 EDT 2005


"C Gillespie" <csgcsg39 at hotmail.com> wrote in message 
news:d6v2s5$blp$1 at ucsnew1.ncl.ac.uk...
> Hi,
>
> Does anyone know of any examples on how (& where) to use staticmethods and
> classmethods?

A python class method is closer to a Java static method than a python static 
method.
Class methods can be useful if you want to be able to change objects in the
class, or if you want to be able to use inheritance, neither of which python 
static
methods allow you to do cleanly.

Another, and rather esoteric use, is to use the class as its own instance.

I  find that static methods are useful when I want to import a class from
a module, and don't want to import the module itself or a lot of bits and
pieces from the module. The class gives a place to park the method, and
also lets you use inheritance, neither of which putting it into the module
does.

John Roth
>
> Thanks
>
> Colin
>
> 




More information about the Python-list mailing list