Everything in Python is an object, and each object can have multiple properties. How does python manage these attributes? Let's take a look.
The __dict__ property of a system object consists of two parts:
Class PropertiesAnd
Object Properties。 The properties of an object may come from the definition of its class, called a class property. Class properties may come from the definition of the class itself or from the parent class. The properties of an object may also be defined by the object instance, called an object property.
object's properties are stored in the object's __dict__ property。 __dict__ is a dictionary, the key is the property name, and the corresponding value is the property itself.
Here is a sample.
Class Bird (object): feather = Trueclass Chicken (bird): fly = False def __init__ (self, Age): self.age = Agesummer = Chicken (2) print (bird.__dict__) print (chicken.__dict__) print (summer.__dict__)
For summer objects. The properties of the object include, class properties: Feather/fly/__init__, Object properties: Age. When we have a summer object, we query the properties of summer object, Chicken class, Bird class, and object class, respectively. You will be able to know all the __dict__ of the summer object, and you will be able to find all the properties that can be called and changed through the object summer.
An attribute can have dependencies between different properties of the same object.
When a property is changed, we want other properties that depend on that property to change at the same time.
At this point, we cannot store properties statically by __dict__. Python provides several methods for generating properties in real time.
One of them is called a feature. Attributes are special properties . For example, we add an attribute adult to the chicken class. Adult is true when the object's age exceeds 1 o'clock, otherwise false:
Class Bird (object): feather = Trueclass Chicken (bird): fly = False def __init__ (self, Age): self.age = Age def getadult (self): if self.age > 1.0:return True else:return False Adult = property (Getadult) c8/># property is Built-insummer = Chicken (2) print (summer.adult) summer.age = 0.5print (Summer.adult)
attribute uses the built-in function property () to create。 The property () can load a maximum of four parameters.
The first three parameters are functions. They are used for querying attributes, altering attributes, and deleting attributes.
The last parameter is an attribute of the document, which can be a string. Play an illustrative role.
We use the following example to further illustrate:
Class num (object): def __init__ (self, value): self.value = value def getneg (self): return-self.value def setneg (self, value): self.value =-value def delneg (self): print ("Value also deleted") del Self.value neg = property (Getneg, Setneg, Delneg, "I ' M negative") x = num (1.1) print (X.NEG) X.neg = -22print (x.value) Print (num.neg.__doc__) del X.neg
The above num is a number, and Neg is a feature. A negative number that is used to represent numbers. When a number is determined, its negative numbers are always determined, and when we change the negative number of a digit, its value should also change. These two points are implemented by Getneg and Setneg.
Delneg, however, assumes that the delete attribute is neg. Then the action that should be run is to delete the property value. The last parameter is the description document for the feature negative.
Using a special method __getattr__ we can use __GETATTR__ (self,name) to query the properties that are generated instantly. when we query an attribute, it is assumed that the property cannot be found by the __dict__ method . Then python invokes the object's __getattr__ method to generate the property on the fly. For example:
Class Bird (object): feather = Trueclass Chicken (bird): fly = False def __init__ (self, Age): self.age = Age def __getattr__ (self, name): if name = = ' Adult ': if self.age > 1.0:return True else:return Fal Se else:raise attributeerror (name) Summer = Chicken (2) print (summer.adult) summer.age = 0.5print (Summer.adult) Print (Summer.male)
each feature needs to have its own handler function. __getattr__ is able to handle all of the immediate generation properties in the same function. __GETATTR__ can handle different properties based on function name differences. For example, when we query the property name male, raise Attributeerror. print (summer.adult) __getattr__ generates adult properties, print (Summer.male) cannot be generated (there are no corresponding build items in __getattr__). Throws an exception. __setattr__ (self, name, value) and __delattr__ (self, name) can be used to modify and delete properties.
They have a wider application and can be used for discretionary properties.
Summarize
__dict__ Tiered Storage properties. The __dict__ of each layer only stores the new properties of the layer. Subclasses do not need to store properties in the parent class repeatedly.
Generating properties on the fly is a concept worth knowing. In the development of Python. It is possible to use this method to manage the properties of objects more rationally.
One into Python deep like the sea--object properties