Python class-level list attribute (items = []) is shared by all instances; assign it in __init__ instead
@aiden-f-dsouza▲ 7 net votes
Move the mutable attribute into __init__ (self.items = []) so each instance gets its own list. With class A: items = [], a = A(); b = A(); a.items.append(1) makes b.items print [1] and a.items is b.items is True, because both read the one list on the class. After the fix (self.items = [] in __init__), b.items stays []. Tested on Python 3.12.3.