-4

From this blog, it seems Python memorize default parameter value during each function call.

http://effbot.org/zone/default-values.htm

But I tested and it seems not? For example, the 2nd function call does not print value of x, y is still False other than memorized value?

def foo(x, y=False):
    if y:
        print x

if __name__ == "__main__":
    foo(100, True) # print
    foo(300) # not print

regards, Lin

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 2
    Your example is not conveying what the article is actually trying to portray. Furthermore, the article is telling you to use `None` as a way to circumvent what is happening. Furthermore, [this](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) also explains what's happening. – idjaw Oct 02 '16 at 00:51
  • 1
    The keyword in what @idjaw linked to is *mutability*. But then again that's also true for the article you linked to... – Andras Deak -- Слава Україні Oct 02 '16 at 01:03

3 Answers3

1

Python doesn't "memorize" anything during each call. It saves one default value once, when the function is defined. In your example, when foo is defined, y has a default value of False. In your first call, you pass in a different value for y. That value is used for that call, but doesn't affect the default. On your second call, you don't explicitly pass anything for y, so the default is used.

The default value doesn't mean "whatever value was used most recently". It's a default value.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
1

The default object for a parameter is determined once and only once, when the function is defined. (I am ignoring the possibility of diffing into the function object from outside and somehow changing the default object.) If the default object is not mutable, as is true of False, end of story.

If the default object is mutable, then its contents can be changed without changing the identity of the object. If the Lundh reference, the default object is a list and a list can be mutated. Each call sees the list in its current state.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • 1
    *The default object for a parameter is determined once and only once, when the function is defined. * -- Actually that is not at all true. The default value is used each time the positional argument is absent. – dawg Oct 02 '16 at 01:01
  • 2
    @dawg What I wrote is exactly true. The default object is *calculated* when the function is defined and bound to the function as part of the __defaults__ attribute (or func_defaults in 2.x) . *Use* of the object when the function is called is a different issue. – Terry Jan Reedy Oct 02 '16 at 01:13
1

The default object is created once.

Maybe this example will simplify the explanation:

>>> import random
>>> 
>>> def func(value=random.random()):
...     print value
...
>>>
>>> func()
0.941870977323
>>> func()
0.941870977323
>>> func(1)
1
>>> func()
0.941870977323
>>>

The default value is an object which is created when the function is defined. In my case, the created object was random value 0.941870977323.

Every time the function is called without arguments, the same default value is used.

When it is called with an argument, then the defaut value is not used.

When is it relevant?

When the default is False, it does not matter, because False is immutable, so it does not matter whether it is an old Falseor a new False.

If the default is something that can change, it is important to understand that it is created only once:

  • in case of random: it is a random value, but always the same random value
  • in case of [] an empty list is created once, but if it is modified by adding elements, the next call will not create a new empty list - it will use the same one.
zvone
  • 18,045
  • 3
  • 49
  • 77