Class Attributes and Instance Attributes
Let's begin with a brief description of the conceptual differences between class attributes and instance attributes:
A class property is a property defined in a class that is bound to that class and accessible to all objects in that class.
Access can be by class name or by instance name.
Instance attributes are data values associated with an instance of a class that are private to that instance and accessible only to that object itself.
When an instance is released, its properties are cleared at the same time.
Let's then look at how Python operates when accessing class attributes and instance attributes through a concrete example.
# Once a class is defined, Python allocates a block of memory space for the class with its associated properties and methods. # Here a class attribute is defined in the class, which is equivalent to having an age attribute with a value of 10 in the memory space of the Person class. class Person(): age = 10 # Adding a function call symbol to a class name is equivalent to creating an object. # Now we've created a man named Shen Teng and a man named Mari, respectively. # # After creation, Python also allocates separate memory spaces for objects st = Person() ml = Person() # To access a class attribute using an object, it first looks inside the object's memory space for the age attribute, and if it's not there, it looks upwards to the memory space of the class the object belongs to. # And here it doesn't take the age attribute directly from the object's memory space, because there is no age attribute in the object's memory space at this point. # Here the object actually accesses the class memory space, takes the value of the age attribute from the class memory space, and prints it out # So here the Shen Teng object attribute value and the Ma Li object attribute value are both class attribute values 10 print("st object: %s" % ) print("ml object: %s" % ) # To remember a point, as long as the object has an assignment operation, then it is equivalent to the object's memory space dynamically created an attribute, so here at this time the Shenteng object's memory space has an age attribute, belongs to the object's attributes, that is, what we call the instance attributes. # Then at this point it will first look for the age attribute in the object memory space, and when it finds it, it won't look for it in the class memory space, so at this point it's the age attribute in the object memory space that's being accessed. # So at this point the Shenton object attribute value is the value of the age attribute of the Shenton object memory space, which is 12. not the class attribute value of 10. = 12 print("st object: %s" % ) # Because the Mari object has not yet performed an assignment operation, it has not yet dynamically created the age attribute in its own memory space. So it is still accessing the value of the age attribute in the class memory space. # And the above statement changes the value of age in Shenten's memory space, so it does not affect the value of the Mari object attribute. # Print the value of the Mari object attribute here which is also the value of the class attribute 10. print("ml object: %s" % ) # Because the assignment statement above changes the memory space of the object, the value of the class attribute does not actually change and remains at the original value of 10. print("Class attribute value: %s" % )
Let's see if the specific printout matches what we analyzed
As you can see, the printed value before there is no no execution are the value of the class attribute 10, and = 12 after the execution, the value becomes 12, and the sum are not changed, is still the class attribute of 10.
Let's understand it again in the form of an illustration:
There is also a point to note that we can initialize the properties of the object in the class constructor __init__, which is also equivalent to an assignment operation on the properties of the object, so it is also in the object's memory space dynamically created instance properties.
Because the self parameter is equivalent to the object itself, after the object is created it is equivalent to the object.age, e.g.,, and the principle of the above example is the same.
class Person(): age = 10 def __init__(self, age): = age # Because the value is assigned to the property in the constructor method. # So when the object is created, the age attribute, which is a property belonging to the object, is present in the memory space of the Shenten object and in the memory space of the Mari object. # So when you use and in the future, all you're accessing is the age value in the object's memory space. st = Person(18) ml = Person(9) # This accesses the value of the age attribute in the object's memory space. # So print 18 and 9 respectively print("st object: %s" % ) print("ml object: %s" % ) # Here to ShenTeng object's age attribute re-assignment, change is also only ShenTeng object memory space of the age attribute value # Does not change the value of the age attribute of the Mari object memory space or the value of the age attribute of the class memory space. = 12 print("st object: %s" % ) print("ml object: %s" % ) print("Class attribute value: %s" % )
Let's look at the printout:
Let's have an illustration of this one too
To summarize:
- Class attributes are present in the class memory space at the time of class creation.
- If the object property is not initialized in the class constructor, the object is created without the property in memory space.
- Instance properties are created dynamically through assignment statements. If they are not dynamically created, accessing a property with the same name as the class through the object will now look up the property in the object's memory space to see if it exists, and if it does not, it will look up the property in the class memory space.
- If an instance property has been dynamically created, then Python's use of the object to access a property with the same name as the class is certain to access the instance property in the object's memory space first.
To this point this article on Python class attributes and instance attributes of the difference between the article is introduced to this, more related to Python class attributes and instance attributes content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!