In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the properties of the Python object". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the properties of the Python object".
Attribute of the _ _ dict__ system
The class attribute of an object may come from its class definition, called a class attribute. Class attributes may come from the class definition itself or may be inherited from the class definition. The object attribute of an object may also be defined by that object instance, which is called object property.
The properties of the object are stored in the _ _ dict__ property of the object. _ _ dict__ is a dictionary, the key is the attribute name, and the corresponding value is the attribute 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.
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__)
Here is our output:
{'_ _ dict__':,'_ _ module__':'_ _ main__','_ _ weakref__':, 'feather': True,' _ _ doc__': None} {'fly': False,' _ _ module__':'_ main__','_ _ doc__': None,'_ _ init__':} {'age': 2}
The first behavior is a property of the bird class, such as feather. The second behavior is the properties of the chicken class, such as the fly and _ _ init__ methods. The third behavior is the property of the summer object, that is, age. Some attributes, such as _ _ doc__, are not defined by us, but are automatically generated 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 of all classes in Python.
As you can see, the attributes in Python are defined hierarchically, for example, there are four layers of object/bird/chicken/summer here. When we need to call a property, Python iterates up layer by layer until we find that property. (a property may be defined repeatedly in different layers, and during the Python upward process, the first one encountered will be selected, that is, the lower-level attribute definition.)
When we have a summer object, we can query the properties of the summer object, the Chicken class, the bird class and the object class, respectively, and we can know that all the _ _ dict__, of the summer object can find all the properties that can be called and modified through the object summer. The following two methods of property modification are equivalent:
Summer.__dict__ ['age'] = 3print (summer.__dict__ [' age']) summer.age = 5print (summer.age)
(in the above case, we already know that the class of the summer object is chicken, and the parent class of the chicken class is bird. If there is only one object, but do not know its class and other information, we can use the _ _ class__ attribute to find the object's class, and then call the class's _ _ base__ property to query the parent class)
Characteristics
There may be dependencies between different properties of the same object. When an attribute is modified, we want other properties that depend on that attribute to change as well. At this point, we cannot store properties statically in the form of _ _ dict__. Python provides several ways to generate properties in real time. One of them is called property. Properties are special properties. For example, we add a feature adult to the chicken class. When the age of the object exceeds 1, adult is True;, otherwise it is 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) # property is built-insummer = chicken (2) print (summer.adult) summer.age = 0.5print (summer.adult)
The property is created using the built-in function property (). Property () can load up to four parameters. The first three parameters are functions, which are used to process query properties, modify properties, and delete properties, respectively. The last parameter is the document of the property, which can be a string for illustrative purposes.
Let's 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) "Ihumm negative") x = num (1.1) print (x.neg) x.neg =-22print (x.value) print (num.neg.__doc__) del x.neg
The num above is a number, while neg is a feature that represents the negative number of a number. When a number is determined, its negative number is always fixed; and when we modify the negative number of a number, its own value should also change. These two points are implemented by getNeg and setNeg. What delNeg means is that if you delete the attribute neg, the action that should be performed is to delete the attribute value. The last parameter of property () ("Ihumm negative") is the documentation for the feature negative.
Use the special method _ _ getattr__
We can use _ _ getattr__ (self, name) to query the instantly generated attributes. When we query a property, if the property cannot be found through the _ _ dict__ method, then Python calls the object's _ _ getattr__ method to generate the property immediately. 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 False 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, and _ _ getattr__ can handle all the just-in-time generated properties in the same function. _ _ getattr__ can handle different properties differently based on the function name. For example, when we query the attribute name male above, raise AttributeError.
(there is also a special _ _ getattribute__ method in Python to query arbitrary properties. _ _ getattr__ can only be used to query attributes that are not in the _ _ dict__ system)
_ _ setattr__ (self, name, value) and _ _ delattr__ (self, name) can be used to modify and delete attributes. They have a wider range of applications and can be used for any attribute.
Other ways to generate attributes in real time
There are other ways to generate properties in real time, such as descriptor (the descriptor class is actually the underlying layer of the property () function, and property () actually creates an object of that class). If you are interested, you can check it further.
Thank you for your reading, these are the contents of "what are the properties of the Python object?" after the study of this article, I believe you have a deeper understanding of what the properties of the Python object have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.