When running following code in python, i create 2 objects of one class, that contains a list object:
class Test():
def run(self):
obj1 = klasse("Werner")
obj2 = klasse("Franz")
print obj1.name
print obj2.name
obj2.add_name("Thomas")
print obj2.name
class klasse(object):
name = [];
def __init__(self, value):
self.name.append(str(value))
def add_name(self, value):
self.name.append(str(value))
if __name__ == "__main__":
Test().run()
this returns in console:
['Werner', 'Franz']
['Werner', 'Franz']
['Werner', 'Franz', 'Thomas']
The Question: it seems to be the same list object in the two created klasse.objects. Whats the solution to isolate the name lists in every created object?
The console output should be:
['Werner']
['Franz']
['Franz', 'Thomas']
The implementation for your
klasse
should be like the following :edit: incorporated @Jon Clements's suggestion.