Please help, not sure what I'm doing wrong here. I've initiated numbers
as an instance variable, not a class variable. Somehow, when appending to x
's numbers, it also gets appended to y
's numbers. Instance variables should be independent of one another, no?
Code
class Foo:
def __init__(self, numbers=[]):
self.numbers = numbers
x = Foo()
y = Foo()
x.numbers.append(3)
print("X NUMBERS")
print(x.numbers)
print("Y NUMBERS")
print(y.numbers)
Output
X NUMBERS
[3]
Y NUMBERS
[3]
N.b - this problem only arises when a default parameter is provided for numbers
in the constructor.