Python class attributes and instance attributes Usage Analysis, python instance
This article analyzes Python class attributes and instance attribute usage. Share it with you for your reference. The details are as follows:
Class property: Class Name. property name
Instance property: instance. property name
>>> Class test ():... ver = 1...> a = test () >>> test. x = 8>. _ dict __{}>>. x8>. x = 9>. _ dict __{ 'X': 9}
1. How is the model of the class property the same? Once the class property is given, all instances will get this value.
2. the attribute values of each instance can be changed.
3. If the attribute of an instance is not displayed, a. x can display the value of this attribute, but it is not in the namespace.
In order to add it to the namespace, it must be explicitly assigned a value.
Class Instant1 (object): count = 0 def _ init _ (self): Instant1.count = Instant1.count + 1 print "created instant" def howmany (self): print Instant1.count print self. countclass Instant2 (object): count = 0 def _ init _ (self): print self. count # print Instant2.count self. count = self. count + 1 # Instant2.count = Instant2.count + 1 print "created instant" def howmany (self): print self. count print Instant2.count
I hope this article will help you with Python programming.