0

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

Consider the following function:

def foo(L = []):
  L.append(1)
  print L

Each time I call foo it will print a new list with more elements than previous time, e.g:

>>> foo()
[1]
>>> foo()
[1, 1]
>>> foo()
[1, 1, 1]

Now consider the following function:

def goo(a = 0):
  a += 1
  print a

When invoking it several times, we get the following picture:

>>> goo()
1
>>> goo()
1
>>> goo()
1

i.e. it does not print a larger value with every call.

What is the reason behind such seemingly inconsistent behavior?
Also, how is it possible to justify the counter-intuitive behavior in the first example, why does the function retain state between calls?

Community
  • 1
  • 1

1 Answers1

2

The default arguments are evaluated once when the function is defined. So you get the same list object each time the function is called.

You'll also get the same 0 object each time the second function is called, but since int is immutable, when you add 1 as fresh object needs to be bound to a

>>> def foo(L = []):
...   print id(L)
...   L.append(1)
...   print id(L)
...   print L
... 
>>> foo()
3077669452
3077669452
[1]
>>> foo()
3077669452
3077669452
[1, 1]
>>> foo()
3077669452
3077669452
[1, 1, 1]

vs

>>> def foo(a=0):
...   print id(a)
...   a+=1
...   print id(a)
...   print a
... 
>>> foo()
165989788
165989776
1
>>> foo()
165989788
165989776
1
>>> foo()
165989788
165989776
1
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • That does not quite answer my question - I don't see why the second example (with an integer parameter) works as it does. –  Aug 04 '11 at 09:02
  • > You'll also get the same 0 object each time the second function is called -- then why can't I get the same [] object each time the first function is called? –  Aug 04 '11 at 09:04
  • 1
    @Grigory: Integers are immutable, `some_int += ...` creates a new integer instead of modifying `some_int`. `a` starts off with the same `0` each time the function is called without argument given, you just go on to change `a` to point to `0 + 1`. –  Aug 04 '11 at 09:04
  • In fact, you're getting the same object each time the first function is called. That's why it "remembers" its previous content. – Michał Bentkowski Aug 04 '11 at 09:06
  • @delnan thanks, that explains everything. –  Aug 04 '11 at 09:06