In the corresponding note, you should know about this trap, which you can see in the near future:
class A: def __init__(self, mylist = []): self.mylist = mylist a = A() a2 = A() a.mylist.append(3) print b.mylist
This confuses many people and is related to how the code is interpreted. Python first interprets the function headers, so it evaluates __init__(self, mylist = []) and saves the link to this list as the default parameter. This means that all instances of A will (unless their own list) be referenced in the original list. The right code to do such a thing would be
class A: def __init__(self, mylist=None): if mylist: self.mylist = mylist else: self.mylist = []
or if you want a shorter expression, you can use the triple syntax:
self.mylist = mylist if mylist else []
source share