I'll take a not-so-wild guess that you currently have something like this:
class MyCollection():
listOfThings = []
def __init__(self):
In this case, listOfThings
is a class attribute, it is shared amongst all instances of the MyCollection
class. Instead, you want an instance attribute, separate for each instance:
class MyCollection():
def __init__(self):
self.listOfThings = []
Edit: so close!
The issue you have is in:
def __init__(self, objs = []):
Here, objs
is known as a "mutable default parameter". The list in the default parameter values is shared for all instances of that class that don't pass anything to __init__
(defining a = MyCollection([])
, for instance, would create a new instance with a separate objs
list).
You should use:
def __init__(self, objs=None):
if objs is None:
objs = []
Canonical question covering this: "Least Astonishment" and the Mutable Default Argument