What is a classmethod for?
Ian Bicking
ianb at colorstudy.com
Mon Jun 3 02:05:13 EDT 2002
On Sun, 2002-06-02 at 19:06, Dennis Peterson wrote:
> > The point is that you can call class methods *without* creating an
> > instance:
>
> >C.foo()
>
> But you can do that with a static method:
>
> class C:
> def foo(x):
> print x
> foo = staticmethod(foo)
>
> C.foo(1)
Maybe a better example would be per-class caching. Like:
class C:
cache = {} # Contains values of (expire_time, cache_value)
def cleanCache(klass):
for key, (expire_time, value) in klass.cache.items():
if expire_time < time.time(): del[klass.cache[key]]
In general it only makes sense when you use class variables -- just like
if you don't use any instance variables it might make more sense to use
a plain function instead of a method.
Ian
More information about the Python-list
mailing list