On Tuesday, January 21, 2014 9:46:16 PM UTC+2, Chris Angelico wrote: > On Wed, Jan 22, 2014 at 6:36 AM, Mû <m...@melix.net> wrote: > > These were clear and quick answers to my problem. I did not think of this > > possibility: the default argument is created once, but accessible only by > > the function, therefore is not a global variable, whereas it looks like if > > it were at first glance. > You can actually poke at the function a bit and see what's happening. > Try this in the interactive interpreter: > >>> def f(x=[2,3]): > x.append(1) > return x > >>> f() > [2, 3, 1] > >>> f() > [2, 3, 1, 1] > >>> f.__defaults__ > ([2, 3, 1, 1],) > > The __defaults__ attribute of a function is a tuple of its parameter > defaults. You can easily see there that the list has changed as you > changed it in the function. You could check it with id() or is, too: > >>> id(f.__defaults__[0]) > 24529576 > >>> id(f()) > 24529576 > >>> f() is f.__defaults__[0] > True > ChrisA
that reminds me C's static :-) def func(y, x = [1]): if y != 1 : func.__defaults__[0][0] = y print(func.__defaults__[0]) func(0) func(2) func(1) [0] [2] [2] p.s. Mu, thanks for question! -- https://mail.python.org/mailman/listinfo/python-list