Instance attributes and class attributes
Theoretical Basis
Class attributes are similar to static variables in C ++. You can directly access them through class name. attribute.
The instance attribute is the attribute that can be used only after the instance of the class is defined.
You cannot access instance attributes using the class name.
You can use the instance to renew class attributes.
If the instance attribute is assigned a value to the class attribute, It is overwritten.
Sample Code
Class syg (object): class_var = 1 def _ init _ (Self): Self. instance_var = 2if _ name _ = '_ main _': syg = syg () # You can reverse the print (syg. class_var) print (syg. class_var) # The instance variable cannot be accessed using the class name # print (syg. instance_var) # The instance attribute overwrites the class attribute syg. class_var = 3 Print (syg. class_var) print (syg. class_var)
Overload object method for instance attribute operations
If you want to control the instance attributes in your class, you can use the following methods to implement special functions:
★These methods cannot operate class attributes.
_ Getattr _ is called to access attributes not defined in the class.
_ Setattr _ called when setting all attributes
_ Delattr _ called When deleting all attributes
_ Getattribute _ is called when accessing all attributes. Include _ getattr __
Sample Code:
Class syg (object): def _ init _ (Self): Self. xx = 0 def _ getattr _ (self, name): Print ("override getattr") return object. _ getattr _ (self, name) def _ setattr _ (self, name, value): Print ("override setattr") return object. _ setattr _ (self, name, value) def _ delattr _ (self, name): Print ('override delattr ') return object. delattr (self, name) def _ getattribute _ (self, name): Print ('override getattribute') return object. _ getattribute _ (self, name) If _ name _ = '_ main _': syg = syg () syg. test = 3 Print (syg. test) print (syg. OK) # This attribute will throw an exception.
Output result:
Override setattr
Override getattribute
3
Override getattribute
Override getattr
Traceback (most recent call last ):
File "Temp. py", line 27, in <module>
Print (syg. OK)
File "Temp. py", line 9, in _ getattr __
Return object. _ getattr _ (self, name)
Attributeerror: Type object 'object' has no attribute '_ getattr __'
Use Property
General function implementation
Class syg (object): def _ init _ (Self): Self. _ x = 0 def getx (Self): Print ("getx") return self. _ x def setx (self, value): Print ('setx') self. _ x = value def delx (Self): del self. _ x # define your own set, get, del operation newx = property (getx, setx, delx) If _ name _ = '_ main __': syg = syg () syg. newx = 3 Print (syg. newx)
Implementation using the decorator Method
class SYG(object): def __init__(self): self._x = 0 @property def var_x(self): print('readonly _x') return self._x @var_x.setter def var_x(self,value): print('setx') self._x = value @var_x.deleter def var_x(self): del self._xif __name__ == '__main__': syg = SYG() syg.var_x = 3 print(syg.var_x)
End of this section