Detailed explanation of Python class attributes and instance attributes
When they first learned Python's object-oriented approach, many students encountered a pitfall: a shared variable was clearly defined in a class, but after assigning a value to an instance, other instances remained motionless. After looking through the documents for a long time, I figured out that it turned out to be the fault of "same name coverage".
Behind this is actually the access priority of instance attributes and class attributes at work. Today’s article uses the shortest possible time to break down these two concepts and explain them clearly, and by the way, I will give you a pitfall avoidance guide.
1. Who are the attributes tied to?
In the object-oriented world of Python, properties are data hanging on an object. But there are two types of "objects" here:
- Class Object: Execution
class Student:automatically created at that moment - Instance Object: Passed
Student()The specific students transferred out
Properties can be attached to these two objects, so there are the following pair of brothers:
2. Instance attributes: everyone’s “private territory”
The biggest feature of instance attributes is that they do not interfere with each other. Changing the properties of one instance will never affect another instance.
2.1 Standard writing:self.xxxexist__init__inside
The most standardized approach is to__init__passed in the constructorselfBinding:
Create two instances, completely independent of each other:
2.2 Dynamic addition is also possible, but be restrained
Python allows you to "stick" new attributes to instances anytime and anywhere:
Although this kind of dynamic attribute is flexible, it can easily mess up the structure when used in production code. It is recommended to only use it in production code.__init__All instance properties are clearly defined here.
3. Class attribute: "Public repository" for all instances
Class attributes are written directly at the top level of the class definition, no needself, any instance (including those that have not yet been created) can access it and points to the same memory by default.
3.1 Definition and Access
All three access methods can get the same value:
3.2 When to use class attributes?
- Storage class-level constants (such as school motto, default configuration)
- Statistics (such as how many instances are currently created)
- State shared by all instances
4. The key point of pitfall: the priority of coverage with the same name
The order in which Python looks for attributes is: Find the instance's own attributes first, and if you can't find them, go to the class. This one rule leads to countless confusing behaviors.
4.1 Demonstration of attributes with the same name
Add an instance with the same namename:
After deleting the instance attribute, access will fall back to the class attribute:
⚠️ The biggest trick here: use
实例.属性名 = 值In the form of "assigning a value" to a class attribute, you are actually quietly creating an instance attribute with the same name, without changing the class layer at all.
4.2 To correctly modify class attributes, you must use the class name
If you really want to modify the values shared by all instances, you must operate through the class name:
5. Classic case: counting the number of instances using class attributes
Test it out:
Note: If written as
self.total_students += 1, it will become adding a private counter to the instance, which is completely useless.
6. Modern writing:@dataclassHow to deal with attributes?
Starting with Python 3.7,@dataclassDecorators can help us automatically generate__init__、__repr__and other methods. However, please note that fields with default values here are instance attributes by default, and class attributes need to be defined separately.
test:
7. See the difference clearly with one picture
8. Three suggestions for avoiding pitfalls
- Try not to let instance attributes and class attributes have the same name - Unless you are really doing advanced operations such as "overwriting", it will only make the code more readable.
- When modifying class attributes, be sure to use the class name——
实例.属性 = 新值Always add a new property with the same name to the instance. - Be careful when dynamically adding instance attributes - it is best to
__init__or@dataclassAll instance properties are explicitly declared here.
After reading this article, if you encounter attribute access problems in the future, first think about this table and the "same name coverage priority", and you can basically locate the cause in seconds 😄

