This article is mainly about sharing the settings of the properties inside the Pyton and getting the built-in functions using: Property Properties:
1.__getattr__ (self, name)
-Defines the behavior when a user attempts to acquire a nonexistent property
2.__getattribute__ (self, name)
-Defines the behavior when the properties of the class are accessed
3.__setattr__ (self, name, value)
-Defines the behavior when a property is set
4.__delattr__ (self, name)
-Defines the behavior when a property is deleted
The property of an object is actually managed by a dictionary table in the object, which can be viewed by object.__dict__, and returns a dictionary table that shows the value corresponding to the string and attribute of each property name;
The above sequence of built-in function calls is:
1. Set the current property, call the __setattr__ (self, Name, value) method to set the current property, if the property exists, directly set the value of the property, if the property does not exist, it will increase the property
2. Gets the value of the property, calls the __getattribute__ (self, name) method to get, if the property exists after the call, gets the value of the property directly, and if the property does not exist, the __getattr__ (self, name) method is called to get
3. Delete attribute call __delattr__ (self, name) method to delete property
It is not difficult to see the setting and invocation of an object's properties by running the following example:
#-*-Coding:utf-8-*-
"""
@author: Zzj
"""
Class PropertyTest:
def __init__ (self, size = 10):
Self.size = Size
def __getattribute__ (self, name):
Print ("Accessing properties of this class")
Return super (). __GETATTRIBUTE__ (name)
def __getattr__ (self, name):
Print ("This property does not exist")
def __setattr__ (self, Name, value):
Print ("Set current Properties")
Return super (). __SETATTR__ (name, value)
def __delattr (self, name):
Print ("Delete Current property")
Super (). __DELATTR__ (name)
def getsize (self):
Return self.size
def SetSize (self, value):
Self.size = value
def delsize (self):
Del self.size
X = Property (GetSize, SetSize, Delsize)
C = PropertyTest ()
Print (c.size)
c.x = 1
Print (c.size)
Print (c.x)
Del c.x
Print (c.size)
The path to Python learning-attributes