September 2026
1051 pages
35h 4m
English
In addition to class methods, there are also attributes that don’t refer to instances of the class, but to the class itself. These attributes can be accessed both via the class itself and via its instances.
The following example defines a class D with a class attribute x:
>>> class D:... x = 10 ...>>> D.x10>>> d = D()>>> d.x10
A class attribute thus can be created directly by an assignment within the body of the class statement. A potential pitfall lies in attempting to assign a new value to the class attribute x, which you access via instance d:
>>> d.x = 20>>> d.x20>>> D.x10
Instead of assigning a new value to the class attribute D.x, a new attribute x was created for instance d with value 20. If you access x via ...
Read now
Unlock full access