If you are performing the initialization on the class, it will affect all instances of the class. If that’s the case, this is not a result of it being in a list, but of it being on the class. For example:
#!/usr/bin/python
class BedrockDenizen():
attributes = []
wilma = BedrockDenizen()
fred = BedrockDenizen()
wilma.attributes.extend(['thin', 'smart'])
fred.attributes.extend(['fat', 'stupid'])
print 'Wilma:', wilma.attributes
print 'Fred:', fred.attributes
You will see that both Fred and Wilma are thin, smart, fat, and stupid.
Wilma: ['thin', 'smart', 'fat', 'stupid']
Fred: ['thin', 'smart', 'fat', 'stupid']
One way to fix this is to put the attribute creation into the init method, so that the attribute is per-instance:
class BedrockDenizen():
def __init__(self):
self.attributes = []
With that change, only Wilma is thin and smart, and only Fred is fat and stupid.
Wilma: ['thin', 'smart']
Fred: ['fat', 'stupid']
You may also need to show us more code. @Bakuriu notes that the problem may be that you are only creating one instance, and he may be right. For example, if this is closer to your code:
class BedrockDenizen():
def __init__(self):
self.attributes = []
neighborhood = [([BedrockDenizen()] * 2) for i in range(2)]
flintstones, rubbles = neighborhood
fred, wilma = flintstones
wilma.attributes.extend(['thin', 'smart'])
fred.attributes.extend(['fat', 'stupid'])
print 'Wilma:', wilma.attributes
print 'Fred:', fred.attributes
Then Fred and Wilma will continue to have the same attributes, because they aren’t really separate people. You may wish to use code more like this:
class BedrockDenizen():
def __init__(self):
self.attributes = []
neighborhood = [[BedrockDenizen() for n in range(2)] for i in range(2)]
flintstones, rubbles = neighborhood
fred, wilma = flintstones
wilma.attributes.extend(['thin', 'smart'])
fred.attributes.extend(['fat', 'stupid'])
print 'Wilma:', wilma.attributes
print 'Fred:', fred.attributes
That depends on what your needs are, though, as it seems like an odd way of doing things without more info.