default method parameter behavior

jianbing.chen at gmail.com jianbing.chen at gmail.com
Wed Apr 2 15:59:25 EDT 2008


I ran into a similar situation like the following (ipython session).
Can anyone please explain why the behavior?
Thanks in advance.

In [11]: def foo(b=[]):
   ....:     b.append(3)
   ....:     return b
   ....:

In [12]: foo()
Out[12]: [3]

In [13]: foo()
Out[13]: [3, 3]

In [14]: foo([])
Out[14]: [3]

In [15]: foo([])
Out[15]: [3]



More information about the Python-list mailing list