Home βΊ Problems βΊ Class variable shared across instances
Class variable shared across instances
π§ Predict the output Topic: oop Difficulty: medium Free
What this problem practises
Object-oriented bugs hide in state: class attributes shared by every instance, mutable defaults declared in __init__ signatures, and methods that mutate self when the caller expected a copy.
This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The snippet
The interactive editor pre-fills exactly this code β read it, then submit your prediction:
class Counter:
count = 0
def increment(self):
Counter.count += 1
a = Counter()
b = Counter()
a.increment()
b.increment()
print(a.count)
print(b.count)Understand the rule
An attribute assigned in the class body belongs to the class itself, and every instance reads it through lookup β so a mutable class attribute like a list is shared: dog1.tricks.append('sit') shows up in dog2.tricks. Assignment through an instance, in contrast, creates a new instance attribute that shadows the class one. Distinguishing read-through from write-shadowing is the whole puzzle.
How to trace it
- Classify each attribute: defined in the class body (class attribute) or via self.x = ... in __init__ (instance attribute).
- Reads fall through: instance first, then class β two fresh instances read the same class attribute.
- Mutations (append, update) on a class attribute act on the one shared object; all instances observe them.
- Assignments (self.x = ...) create an instance attribute that shadows the class one for that instance only.
Common wrong fixes
- Using a class-level list as a per-instance default instead of assigning a fresh list in __init__.
- Expecting dog1.count += 1 to change the class attribute; it reads the shared value then writes an instance copy.
- Confusing this with the mutable-default-argument trap β related mechanism, different location.
Rule to remember
Class attributes are shared; instance assignment shadows them. Mutating a shared object affects every instance β per-instance state belongs in __init__.
πΌ Learning path: Interview Practice
Problem 2 of 16 in this path. Work them in order β each one builds on the rule before it.
β Mutable default argument trapNext in path: List copy vs reference β
More oop problems
- Counter object adds up medium
- Duck overrides speak() medium
- AttributeError: missing self medium
Keep going
- Output-prediction strategies β how to predict tricky snippets without running them.
- Browse every oop problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.