What is class method?
Hrvoje Niksic
hniksic at xemacs.org
Sun Aug 24 05:09:46 EDT 2008
Hussein B <hubaghdadi at gmail.com> writes:
> Hi,
> I'm familiar with static method concept, but what is the class method?
> how it does differ from static method? when to use it?
> --
> class M:
> def method(cls, x):
> pass
>
> method = classmethod(method)
Use it when your method needs to know what class it is called from.
This makes sense in the context of subclassing:
class M(object):
@classmethod
def method(cls, x):
print cls, x
class N(M):
pass
>>> M.method(1)
<class '__main__.M'> 1
>>> N.method(1)
<class '__main__.N'> 1
More information about the Python-list
mailing list