I have a code that creates an object inside a loop. The object has a list that contains different type of object.
class A:
a = ''
def __init__(self):
pass
class B:
def __init__(self):
pass
list_of_A = list()
for i in range(3):
_b = B()
for j in range(2):
_a = A()
_b.list_of_A.append(_a)
print(len(_b.list_of_A))
The output is: 2 4 6
What I expected was: 2 2 2
I tried deleting _b at the end of the inner loop. But didn't work.
How should I make sure the loop creates a new object of B.
list_of_A = list()into__init__and prependself., i.e.def __init__(self): self.list_of_A = list(). BTW,list()can be[].