Python traps · Chapter 8 of 16
A class attribute shared between instances
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Python traps are free. This one comes with the all labs plan.
Why you need this
The same trick with a number behaves differently:
class Printer:
labels = 0
def print_label(self):
self.labels += 1
a = Printer()
b = Printer()
a.print_label()
a.print_label()
print(a.labels, b.labels, Printer.labels)
print(vars(a))
2 0 0
{'labels': 2}
self.labels += 1 reads labels from the class, adds one, and assigns the result to self.labels, which creates a name on the object. An assignment to self.x never changes the class. An append is not an assignment: it changes the object it found, and it found the class's list. That is why a counter on the class seems safe and a list on the class does not.
The fix is to make the list where each object is made, in __init__. A dataclass does it for you with field(default_factory=list), and it refuses a plain [] default (case 1's trap) with a ValueError.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: Santosh's stop on Imran's sheet
Write reproduce() in repro.py. Make two riders with Rider(name, area) from riders.py (any names and areas you like), assign the stop "D2041" to the first rider only with assign, and return the second rider's list of stops, rider.stops.
The check runs your reproduce() twice: beside the original riders.py it must return ['D2041'] (the first rider's stop turned up on the second), and beside a fixed riders.py it must return []. So it must really make the two riders and assign the stop. No real waiting and no input(): reproduce() must finish within 3 seconds.
- Task 2
riders.py: every rider gets their own stops
Fix the Rider class in riders.py. A new Rider(name, area) must start with an empty list of stops, however many riders came before it and whatever they were assigned. rider.assign(stop) adds the stop to the end of that rider's stops only, and rider.stops is that rider's own list. After any number of riders and stops, the Rider class itself holds no stops.
Keep route() (for example "Santosh Kamble (Andheri): D2041, D2044") and the line assign prints (rider <name>: assigned <stop>) as they are. The check imports a fresh copy of your riders.py and makes its own riders, and a fix that empties the shared list when the next rider is made is as wrong as the bug: the earlier riders lose their stops.
- Task 3
test_riders.py: a test that would have caught it
Write a pytest file, test_riders.py, that would have caught this bug before it reached the riders. The check runs your file three times: beside the original riders.py, where at least one test must fail; beside the fixed riders.py, where every test must pass; and beside a wrong fix whose __init__ empties the shared class list, where at least one test must fail.
So check both riders: a rider made after another one got a stop has no stops, and the earlier rider still has exactly its own stops after the next rider is made. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: Santosh's stop on Imran's sheet
- riders.py: every rider gets their own stops
- test_riders.py: a test that would have caught it
More in Everyday traps
Build "A class attribute shared between instances" yourself
DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python traps are free. This one comes with the all labs plan.