Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the details about python class properties and instance properties

2025-01-27 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 details about python class attributes and instance attributes". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what are the details about python class attributes and instance attributes"!

For Python class attributes and instance attributes, simply put, class attributes are attributes defined at the same level as class methods when defining classes; instance attributes are attributes defined at__init__instance initialization.

From the point of view of instance, class attributes and instance attributes can be accessed and updated in the instance by using the method of self. Attribute name. Compared with local attributes in the instance method, the attribute can be modified globally in the instance, and even mixed in many cases.

In fact, class attributes can also be directly used class name. The class attribute name method is accessed directly, even if it is not instantiated. This is an unwanted class that doesn't import and takes up memory space, even if that class isn't instantiated. At the same time, the memory space of this class attribute method is shared by all instantiated objects. Theoretically, instance object A updates the class attribute, and this attribute in B will also be updated. Some aspects implement cross-instance global variables, which can be used as instances for direct interaction.

There are some details to note here, that is, if the class attributes are int,string,float,tuple such immutable types, there is actually a new one for each instance change, and the original will not be modified. The class attributes of these types are the same as the instance attributes; while the variable type dict set list, as well as the instance object; these are truly global updates, only one instance object of the class is modified, and all instance objects of the class will change.

Finally, it's easy to overlook that if you're using Python Multiprocessing, don't try to communicate with these class attributes because one instance may be reading and another modified. Or queue, which is thread-safe.

The following code example illustrates this.

class TreeNode: intItem= 5 StringItem = 'Test' listItem = [1,2,3,4] Dict = {1:2,2:4} def __init__(self, x): self.Intval = x self.listval= ['A','B']A = TreeNode(1)B = TreeNode(2)print ("TreeNode.intItem:%s, TreeNode.listItem:%s" %(TreeNode.intItem,TreeNode.listItem))print ("A:%s, B:%s" %(A.intItem, B.intItem))print ("A:%s, B:%s" %(A.StringItem, B.StringItem))print ("A:%s, B:%s" %(A.listItem, B.listItem))print ("A:%s, B:%s" %(A.Dict, B.Dict))print ("A:%s, B:%s" %(A.listval, B.listval))A.intItem = 8A.StringItem = 'Test2'A.listItem.insert(-1,5)A.Dict[3] = 9A.listval.insert(-1,5)print ("========only update A=========")print ("TreeNode.intItem:%s, TreeNode.listItem:%s" %(TreeNode.intItem,TreeNode.listItem))print ("A:%s, B:%s" %(A.intItem, B.intItem))print("A:%s, B:%s" %(A.StringItem, B.StringItem))print("A:%s, B:%s" %(A.listItem, B.listItem))print("A:%s, B:%s" %(A.Dict, B.Dict))print("A:%s, B:%s" %(A.listval, B.listval)) At this point, I believe that you have a deeper understanding of" What are the details about python class attributes and instance attributes?", you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report