K
K. Jansma
Hi,
given the following example class
class Test:
def f(self,a, L=[]):
L.append(a)
return L
and the following statements
a = Test()
a.f(0)
a.f(0)
a.f(0)
b = Test()
b.f(0)
this is the output I would like to have (i.e., expect)
But this is what I get:
as you can see, the b.f method shares L with a.f.
How can I avoid this without using eg. self.L in an __init__?
Thanks in advance,
Karel.
given the following example class
class Test:
def f(self,a, L=[]):
L.append(a)
return L
and the following statements
a = Test()
a.f(0)
a.f(0)
a.f(0)
b = Test()
b.f(0)
this is the output I would like to have (i.e., expect)
[0]a = Test()
a.f(0) [0]
a.f(0) [0, 0]
a.f(0) [0, 0, 0]
b = Test()
b.f(0)
But this is what I get:
[0, 0, 0, 0]a = Test()
a.f(0) [0]
a.f(0) [0, 0]
a.f(0) [0, 0, 0]
b = Test()
b.f(0)
as you can see, the b.f method shares L with a.f.
How can I avoid this without using eg. self.L in an __init__?
Thanks in advance,
Karel.