default method parameter behavior

J

jianbing.chen

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]
 
K

Konstantin Veretennicov

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

Of course.

Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that that same ``pre-computed'' value is used
for each call. This is especially important to understand when a
default parameter is a mutable object, such as a list or a dictionary:
if the function modifies the object (e.g. by appending an item to a
list), the default value is in effect modified.
 
J

Jerry Hill

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

http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects

Since you got bitten by this, you may also find it useful to take a
look at one of the pages that talks about common python problems, like
http://zephyrfalcon.org/labs/python_pitfalls.html or
http://www.ferg.org/projects/python_gotchas.html (both of which
mention the problems with mutable default arguments).
 
P

Primoz Skale

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]

I think it has something to do with foo.func_defaults thing :) If parameter
that is assigned a default value is mutable then foo.func_defaults is
changed everytime there is no parameter passed to the function.

For example:
b.append(3)
return b
f.func_defaults #default is [], because function was not yet called ([],)
f() #no args [3]
f.func_defaults #default is now b=[3], because b is mutable object ([3],)
f([]) #empty; default still == [3] [3]
f.func_defaults #as we can see here ([3],)
f() #again no args; default is changed to [3,3] [3, 3]
f.func_defaults ([3, 3],)
f([]) #no args [3]
f.func_defaults
([3, 3],)


As *I* understand it, it goes something like this. Because mutable objects
change globaly if not passed correctly, and because [] is mutable, python
creates a *def global* object, which is only seen by function that created
it, with a name b to which it assigns a default value of []. But when this
default [] is changed in function, it becomes [3], and so on.....

Correct me if I am wrong...

P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top