I created a class:
class A: aList = []
I now have a function that instantiates this class and adds elements to the aList list.
Note: there are 2 items
for item in items: a = A(); a.aList.append(item);
I found that the first A and second object A have the same number of elements in their aList. I would expect the first object A to have the first element in its list, and the second object A to have the second element in its aList list.
Can someone explain how this happens?
PS:
I manage to solve this problem by moving aList inside the constructor:
def __init__(self): self.aList = [];
but I'm still interested in this behavior
python
zfranciscus
source share