First, Staticmethod (function)
Return a static method for function.
A static method does not receive an implicit first argument. To declare a static method with this idiom:
class Sing (): @staticmethod def SayHello (): print"Hello World " def Sayno (): print"no " if __name__ ' __main__ ' : Sing.sayhello () sing.sayno ( )
Sing.sayno error is as follows:
Typeerror:unbound method Sayno () must is called with Sing instance as first argument (got nothing instead)
Sayno () method is not a static method, you need to instantiate the object sing before calling the method
and the SayHello () method does not error, because the Staticmethod modification
The following method is the right one:
class Sing (): @staticmethod def SayHello (): print " hello World " def Sayno (self): print " no " __name__ = = " __main__ " : Sing.sayhello () Sing = Sing () Sing.sayno ()
Second, Classmethod (function)
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 Sing (): @classmethod def SayHello (self): print " hello World " def Sayno (self): print " no " __name__ = = " __main__ " : Sing.sayhello () Sing = Sing () Sing.sayno ()
Note: The Staticmethod modified method does not need to be connected to the self parameter, while the method of Classmethod modification requires the self parameter;
Third, @property modifiers (the new class is unique, the new class is the class that the object defines)
The properties of the class are decorated as follows for the definition of the class without the @property modifier:
class C (object): def =< Span style= "color: #000000;" > None def Getx (self): return self._x def Setx (self, value): self._x = value d EF del self._x x = Property (Getx, Setx, Delx, " i ' M the ' X ' property. )
Definition of a class decorated with the @property modifier: (Introduction of Setter,deleter,getter property starting from Python2.6)
class C (object): def __init__ (self): self._x = None @property def x (self): "" "I ' m the ' x ' property. """ return self._x @x.setter def x (Self, value): = value @ X.deleter def x (self): del self._x
Staticmethod Classmethod modifier