0

Look a the following iPython session

In [8]: def fun(a=[]):
   ...:     a.append(7)
   ...:     return(a)

In [9]: fun()
Out[9]: [7]

In [10]: fun()
Out[10]: [7, 7]

In [11]: fun(a=[0])
Out[11]: [0, 7]

In [12]: fun()
Out[12]: [7, 7, 7]

What I was expecting was that each time I run a function is uses a fresh instance of an argument. But it seems it is evaluated only at the first call. The next calls they are treated like static members in C++. But if you run the function with explicitly setting the argument to something new you get the right answer. But if you run it again without you get the old object.

What is the rationale behind?

I was expecting that the function argument is evaluated fresh each time, and when the function finishes it gets destroyed.

Met
  • 298
  • 2
  • 6

0 Answers0