Python is all objects (object), and each object can have multiple attributes. Python's attributes have a unified management solution.
__dict__ System of attributes
The properties of an object may come from its class definition, called the class attribute. Class properties may come from the class definition itself, or they may inherit from the class definition. An object's properties may also be defined by the object instance, called an object attribute.
The properties of the object are stored in the object's __dict__ property. __dict__ is a dictionary, the key is a property name, and the corresponding value is the property itself. Let's look at the following classes and objects. The chicken class inherits from the Bird class, and summer is an object of the chicken class.
Copy Code code as follows:
Class Bird (object):
Feather = True
Class Chicken (bird):
Fly = False
def __init__ (self, Age):
Self.age = Age
Summer = Chicken (2)
Print (bird.__dict__)
Print (chicken.__dict__)
Print (summer.__dict__)
Here are the results of our output:
Copy Code code as follows:
{' __dict__ ': <attribute ' __dict__ ' of ' bird ' objects>, ' __module__ ': ' __main__ ', ' __weakref__ ': <attribute ' __ weakref__ ' of ' bird ' objects>, ' feather ': True, ' __doc__ ': None}
{' Fly ': False, ' __module__ ': ' __main__ ', ' __doc__ ': None, ' __init__ ': <function __init__ at 0x2b91db476d70>}
{' Age ': 2}
The first act bird the properties of the class, such as feather. The second behavior chicken the properties of the class, such as the Fly and __init__ methods. The third act summer the object's properties, which is age. Some attributes, such as __doc__, are not defined by us, but are generated automatically by Python. In addition, the bird class also has a parent class, which is the object class (as our bird definition, class bird (object)). This object class is the parent class for all classes in Python.
As you can see, the attributes in Python are hierarchically defined, for example, this is divided into the four layers of object/bird/chicken/summer. When we need to invoke a property, Python traverses the layer up and down until it finds that attribute. (a property may appear in a different layer to be repeatedly defined, Python up the process, you will choose the first encountered, that is, to compare the lower-level attribute definitions).
When we have a summer object, we query the properties of the summer object, the chicken class, The bird class, and the object class, and we can know all summer of the __dict__ object, You can find all the properties that can be invoked and modified by object summer. The following two property modification methods are equivalent:
Copy Code code as follows:
summer.__dict__[' age ' = 3
Print (summer.__dict__[' age '])
Summer.age = 5
Print (Summer.age)
(In the above case, we already know that the class of the summer object is chicken, and the chicken class's parent class is bird.) If there is only one object, without knowing its class and other information, we can use the __class__ property to find the object's class and then invoke the class's __base__ property to query the parent class.
Characteristics
There may be dependencies between the different attributes of the same object. When a property is modified, other properties that we want to depend on the property also change. At this point, we cannot store properties statically by __dict__ the method. Python provides a variety of ways to instantly generate properties. One of these is called an attribute. Attributes are special properties. For example, we add a feature adult to the chicken class. Adult is true when the age of the object exceeds 1 o'clock, otherwise false:
Copy Code code as follows:
Class Bird (object):
Feather = True
Class 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) # property is built-in
Summer = Chicken (2)
Print (Summer.adult)
Summer.age = 0.5
Print (Summer.adult)
Attributes are created by using the built-in function property (). The property () can load up to four parameters. The first three parameters are functions that handle query characteristics, modify attributes, and delete attributes. The last parameter is a document of the attribute, which can be used as a string to illustrate the effect.
We use the following example to further illustrate:
Copy Code code as follows:
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 =-22
Print (X.value)
Print (num.neg.__doc__)
Del X.neg
The num above is a number, and neg is an attribute that represents a negative number of numbers. When a number is determined, its negative numbers are always fixed, and when we modify a negative number, its own value should change. These two points are implemented by Getneg and Setneg. And Delneg indicates that if you delete an attribute neg, the action you should perform is to delete the property value. The last parameter ("I ' M negative") of the property () is a descriptive document for the attribute negative.
Using special methods __getattr__
We can use __getattr__ (self, name) to query for instantly generated properties. When we query a property, if the property cannot be found through the __dict__ method, Python invokes the object's __getattr__ method to generate the property instantly. Like what:
Copy Code code as follows:
Class Bird (object):
Feather = True
Class 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 False
Else:raise Attributeerror (name)
Summer = Chicken (2)
Print (Summer.adult)
Summer.age = 0.5
Print (Summer.adult)
Print (Summer.male)
Each feature needs its own handler function, and __getattr__ can put all the instant-generated properties in the same function. __GETATTR__ can handle different properties differently depending on the function name. For example, when we query the property name male, raise Attributeerror.
(There is also a __getattribute__ special method in Python to query for arbitrary properties.) __GETATTR__ can only be used to query properties that are not in the __dict__ system)
__setattr__ (self, name, value) and __delattr__ (self, name) can be used to modify and delete properties. They are more widely applied and can be used with arbitrary properties.
Other ways to instantly generate properties
Instant-generated properties can also be used in other ways, such as descriptor (the descriptor class is actually the bottom of the property () function, which actually creates an object of that class). Be interested in further inspection.
Summarize
__dict__ Tiered Storage properties. The __dict__ of each layer stores only the new properties for that layer. Subclasses do not need to repeatedly store properties in the parent class.
Instant-generated properties are concepts that are worth understanding. In Python development, it is possible to use this approach to manage the properties of objects more reasonably.