H
henrikpierrou
Hi,
How do python instances work?
Why does the code at the end of my posting produce this output:
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
instead of
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[]
----------------------------
class MyClass:
list = []
def add(self, x):
self.list.append(x)
def printer(self):
print self.list
a = MyClass()
b = MyClass()
for n in range(10):
a.add(n)
print "list in a:"
a.printer()
print "list in b:"
b.printer()
/H
How do python instances work?
Why does the code at the end of my posting produce this output:
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
instead of
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[]
----------------------------
class MyClass:
list = []
def add(self, x):
self.list.append(x)
def printer(self):
print self.list
a = MyClass()
b = MyClass()
for n in range(10):
a.add(n)
print "list in a:"
a.printer()
print "list in b:"
b.printer()
/H