This is a case of reference. Since the list items
is mutable (can be directly changed with an internal method), any change you make to it in any function is reflected in all references to it.
For example, if you have the following code:
def f(x):
x.append(5)
a = [1, 2, 3]
f(a)
# a is now [1, 2, 3, 4], even in the global scope
This occurs because a
is a mutable list, and so it is passed by reference.
So when you use items = []
, you are creating a blank list when the program is started, but not every time you create a new instance. Instead, each instance refers to the same list, created when the class was "declared". So, since each instance refers to the same list, they are all changed.
To fix this, change your constructor to:
def __init__(self, ID=str(), items=None): # you can also use '' instead of str()
if not items: items = []
# everything else
A few good links to explain this better/in a different way:
There are a ton of other questions like this out there, just search [python] None as default argument
.