You're making a common Python newcomer mistake.
See my answer here:
How should I declare default values for instance variables in Python?
Briefly explained, Python interprets the class definitions only once. That means everything declared in the __init__()
method is only created once. Or, in another words, your []
list default argument is only made once.
Then self.l = l
assigns a reference to the same instance every time you create a new class, hence the behaviour you weren't expecting.
The Pythonic way is this (partial code):
def __init__(self, arg=None):
if arg is None:
arg = []
self.arg = arg
Also, you should consider using a better naming convention than l
, which is hard to read and might be mistaken as 1
or |
.