Module 11 · Object-Oriented Programming — topic 10 of 25
Class variables
Variables shared by all instances of a class: constants, counters and defaults, reading them through instances or the class, and the trap of modifying them through self.
Learning objectives
- Define and use class variables
- Update shared state correctly through the class
- Avoid shadowing class variables by assigning through self
Definition
A class variable is defined directly in the class body. It belongs to the class object and is shared by all instances; reading obj.name finds it if the instance has no attribute of that name.
Introduction
Some data belongs to the concept, not to individual objects: the interest rate for all savings accounts, or how many students have been created. That data goes in class variables.
Why it matters
Class variables avoid repeating the same value in every object and give one place to change shared configuration — but writing to them through self is a classic bug.
Detailed explanation
Variables in the class body (outside methods) are class variables: rate = 0.04.
They can be read as ClassName.rate or self.rate; the instance lookup falls back to the class.
To change the shared value, assign through the class: SavingsAccount.rate = 0.05 or type(self).rate = ....
Assigning self.rate = ... creates an instance variable that shadows the class variable for that object only.
Typical uses: constants (MAX_SIZE), counters of created objects, default settings and lookup tables.
Mutable class variables (lists, dicts) are shared by every instance — usually unintended unless it really is a shared registry.
Syntax
- Defined in the class body.
- Read via class or instance.
- Write via the class.
Code examples
Shared values and counters
KENHOLT KENHOLT 2
Changing the shared value
0.05 0.05
More examples
The self-assignment trap
[1, 2, 3] 3 [1, 1, 1] 0
Step-by-step explanation
- Ticket updates the counter through the class, so each new ticket sees the incremented value.
- In BuggyTicket, self.sold += 1 reads the class value 0, adds 1 and stores the result on the instance.
- Every buggy ticket therefore gets number 1, and the class counter never changes.
Expected output
Running “The self-assignment trap” prints exactly:
Practical applications
- Configuration defaults on base classes in frameworks.
- Registries of plugins or subclasses.
Best practices
- Use class variables for constants and genuinely shared data.
- Name constants in UPPER_CASE.
- Modify shared values through the class name.
Common mistakes
Incrementing via self
Avoid
Prefer
Assignment through self creates an instance attribute.
Shared mutable list
Avoid
Prefer
One list would be shared by every group.
Important notes
- type(self).count is preferred over the class name when subclasses should have their own counters.
Key takeaways
- Class variables are shared by all instances.
- Read them through the instance or the class.
- Write through the class, not self.
Try it yourself
- 1Give an Order class an auto-incrementing order number using a class variable.
- 2Store a tax rate as a class variable and show how changing it affects all products.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?