What is class method?
MeTheGameMakingGuy
gopsychonauts at gmail.com
Sun Aug 24 04:35:58 EDT 2008
On Aug 24, 6:32 pm, Hussein B <hubaghd... at gmail.com> wrote:
> 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)
> --
> Thank you for your time.
Firstly, don't use method = classmethod(method). Decorators are far
better. The following code has the same effect:
class M:
@classmethod
def method(cls, x):
pass
Far more readable, right?
Class methods are useful if you've got lots of inheritance happening.
The first argument passed in is the class calling the method. Handy
for a mix-in: it can add methods affecting the actual class it's mixed
into, rather than messing with the mix-in itself.
More information about the Python-list
mailing list