share function argument between subsequent calls but not between class instances!

K. Jansma spam_me at yahoo.com
Sat Feb 18 11:42:54 EST 2006


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)

>>> a = Test()
>>> a.f(0)
[0]
>>> a.f(0)
[0, 0]
>>> a.f(0)
[0, 0, 0]
>>> b = Test()
>>> b.f(0)
[0]

But this is what I get:

>>> a = Test()
>>> a.f(0)
[0]
>>> a.f(0)
[0, 0]
>>> a.f(0)
[0, 0, 0]
>>> b = Test()
>>> b.f(0)
[0, 0, 0, 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.




More information about the Python-list mailing list