self understanding
Popular understanding of self is an instance of an object, what is instantiated, self is what
Examples of variables: variables can be used through instantiation
class the Person (): DEF the __init__ (Self, ID, name): # constructor self.id ID = # instance variables of the self.name name = # instance variables DEF Cook (Self): Print ( ' % S in cooking ' % the self.name) DEF housework (Self): Print ( ' % S housework ' % the self.name) XH = the Person (. 1, ' black ' ) # is equivalent to __init (self, id, name) self actually XH xh.cook () xh.housework() XB = the Person (2, ' white ' ) xb.cook()
Class variables: variables which are defined in the class, priority access to the object class instance variables, i.e. variables of the constructor, if the acquired class variables will not obtain
class Test: name = 'haha' def __init__(self,name): # self.name = name pass def test(self): print('姓名是%s'%self.name) t=Test('ahah') print(t.name)
Class methods: instance methods can not be called, the class name can be called directly, examples can also be called class methods can be called a class variable
class Test: name = 'haha' def __init__(self,name): # self.name = name pass DEF Test (Self): Print ( ' name is S% ' % the self.name) @classmethod # plus the decorator is to define a class method DEF sayCountry (CLS): # CLS itself representative of this class 'the Test' Print (cls.name) t=Test('ahah') print(t.name) Test.sayCountry () # class method, no examples of direct call by the class name; examples can also call with
Static method: You can not call the class variables, class methods can not be called; the class name can be called directly, examples can also call it directly
class Test: name = 'haha' def __init__(self,name): self.name = name DEF Test (Self): Print ( ' name is S% ' % the self.name) @classmethod # plus the decorator is to define a class method DEF sayCountry (CLS): # CLS itself representative of this class 'the Test' Print (cls.name) @classmethod def getCountry(cls): cls.sayCountry () # class methods can invoke another @staticmethod # plus the decorator is to define a static method, can not be called class variables and class methods DEF Help (): Print ( ' manual ' ) t=Test('ahah') print(t.name) Test.sayCountry () # class method, no examples of direct call by the class name; with examples can also call Test.help () Test.getCountry()
Attribute methods: one looks like variable parameters can not
class Test: name = 'haha' def __init__(self,name,age): self.name = name self.age = age DEF Test (Self): Print ( ' name is S% ' % the self.name) @classmethod # plus the decorator is to define a class method DEF sayCountry (CLS): # CLS itself representative of this class 'the Test' Print (cls.name) @classmethod def getCountry(cls): cls.sayCountry () # class methods can invoke another @staticmethod # plus the decorator is to define a static method, can not be called class variables and class methods DEF Help (): Print ( ' manual ' ) @property DEF . price (Self): # property methods, can not use the parameter Print (self.age) t=Test('ahah',10) print(t.name) Test.sayCountry () # class method, no examples of direct call by the class name; with examples can also call Test.help () Test.getCountry() t.price # without parentheses, reference and reference attributes (variables) consistent