This article mainly introduces the usage of Python class attributes and instance attributes, and analyzes the functions, definitions, and usage skills of Python class attributes and instance attributes, for more information about Python class attributes and instance attributes, see the following example. 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>>> a.__dict__{}>>> a.x8>>> a.x=9>>> a.__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.