Static Methods
Only nominal collation management, in fact, no property in the class or instance can be accessed in a static method
The method of decorating is changed into a static method by @staticmethod Adorner, what is static method? In fact, it is not difficult to understand, the ordinary method can be called directly after instantiation, And in the method can pass self. Invokes an instance variable or a class variable, but a static method cannot access an instance variable or a class variable, and a method that cannot access an instance variable or a class variable is, in fact, nothing to do with the class itself, and its only association with a class is the need to call this method through the class name
Class Dog (object): def __init__ (self,name): self.name = name @staticmethod #实际上跟类没什么关系了 def Eat (self): print ("%s is eating%s"% (self.name, "BB")) d = Dog ("AA") D.eat (d)
class Method
Class methods are implemented through the @classmethod adorner, the difference between a class method and a common method is that class methods can only access class variables and cannot access instance variables
Class Dog (object): n = 333 def __init__ (self,name): self.name = name # @staticmethod #实际上跟类没什么关系了 @classmethod def Eat (self): print ('%s is eating%s '% (SELF.N, "BB") def talk (' self ') : print ("%s is Talking "%self.name) d = Dog (" AA ") D.eat ()
Python Object-oriented