Classmethod (function)
English Description:
Classmethod is used to specify a method of a class as a class method, without this parameter specifying the class method as an instance method, using the following method:
Class C: @classmethod def f (CLS, arg1, Arg2, ...): ...
The class method can be called either directly from the Class (C.F ()) or as an instance call (C (). f ()).
Version: added in python2.2, new features added in python2.4. Still available in Python3.
English Description:
Return A class method for function.
A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method with this idiom:
Class C: @classmethod def f (CLS, arg1, Arg2, ...): ...
The @classmethod form is a function decorator–see the description of function definitions on function definitions for de Tails.
It can be called either on the class (such as C.F ()) or in an instance (such as C (). f ()). The instance is ignored except for its class. If A class method is called for a derived class, the derived class object is passed as the implied first argument.
Class methods is different than C + + or Java static methods. If you want those, see Staticmethod ().
For more information on class methods, consult the documentation on the standard type hierarchy in the standard type Hiera Rchy.
New in version 2.2.
Changed in version 2.4:function decorator syntax added.
code example:
>>> class C: ... @classmethod ... def f (self): ... Print "This is a class method" ...>>> c.f () The Is a class method>>> C = C () >>> C.F () the is a C Lass method>>> class D: ... def f (self): ... Print "This isn't a class method" ...>>> D.F () Traceback (most recent call last): File "
", Lin E 1, in
Typeerror:unbound method F () must is called with D instance as first argument (got nothing instead ) >>> d = d () >>> D.F () This is not a class method