The problem is in this line:
def __init__(self, data = []):
When you write data = [] to set an empty list as the default value for this argument, Python creates the list only once and uses the same list for every method call without an explicit data argument. In your case, this happens when you create both a and b , since you do not specify an explicit list to any of the constructors, so both a and b use the same object as their data list. Any changes you make to one will be reflected in the other.
To fix this, I would suggest replacing the first line of the constructor with
def __init__(self, data=None): if data is None: data = []
source share