0

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

The following code illustrates the issue:

def fn(param, named_param={}, another_named_param=1):
    named_param[param] = str(another_named_param)
    another_named_param += param
    return named_param

for i in range(0, 2):
    result = {}
    result = fn(i)

    print result

print

for i in range(0, 2):
    result = fn(i, named_param={})

    print result

print

result = fn(0)
print result
result = fn(1)
print result

Output:

{0: '1'}
{0: '1', 1: '1'}

{0: '1'}
{1: '1'}

{0: '1', 1: '1'}
{0: '1', 1: '1'}

I expected the output of the 1st, 2nd loop and subsequent 2 single calls with param matching the values of the for loop would have the same textual output, but fn holds onto the value of named_param if not explicitly defaulted to an empty dictionary. Is functionality defined in the documentation?

Community
  • 1
  • 1
Nate Pinchot
  • 3,288
  • 24
  • 35

1 Answers1

1

The default value of named_param is evaluated once, when the function definition is executed. It is the same dictionary each time and its value is retained between calls to the function.

Do not use mutable objects as default values in functions unless you do not mutate them. Instead, use None or another sentinel value, and check for that value and replace it with a fresh object (e.g. empty dictionary). This way you get a fresh one each time your function is called.

kindall
  • 178,883
  • 35
  • 278
  • 309