What is a classmethod for?

Aahz aahz at pythoncraft.com
Sun Jun 2 13:46:46 EDT 2002


In article <addgv30f4d at enews4.newsguy.com>,
Dennis Peterson <denpeterson at yahoo.com> wrote:
>
>class C:
>    def foo(cls):
>        print cls
>    foo = classmethod(foo)
>
>class D(C):
>    pass
>
>c = C()
>d = D()
>c.foo() prints __main__.C
>d.foo() prints __main__.D
>
>That's nice, but I can't think of a use for it, given that we have
>inheritance and isinstance(). Can anyone enlighten me?

The point is that you can call class methods *without* creating an
instance:

C.foo()
D.foo()

This allows you to manipulate class variables through methods, rather
than using functions or needing to create an instance.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In the end, outside of spy agencies, people are far too trusting and
willing to help."  --Ira Winkler



More information about the Python-list mailing list