The following code doesn't work the way I intended it to:
class A:
def __init__(self, depth=0, places=list()):
print(len(places))
if depth < 3:
places.append(2)
a = A(depth + 1)
a = A()
How I'd expect this to work is that it creates three instances of class A
each with a list called places with one element, 2
. Instead, I get three elements, with the list [2]
, [2, 2]
, [2, 2, 2]
, respectively.
I'm assuming that creating an object of a class within that class this way causes the variables to act statically but I'm not sure why and how to write this code the way I'd like it to work.