where dos the default agument live in? local name spaces or gloabal namespaces or else?

Matt Gerrans matt_gerrans at hp.com
Mon Aug 19 18:17:16 CEST 2002


> In the first example, if you call f with only one argument, the default
> argument will be the _same object_ for each invocation, and so same list
> will be appended to each time.  This is, in almost all cases, not the
> intended behavior.

Ah, that is the crux of the matter.

>>> def f( a, L = [] ):
 L.append(a)
 return L

>>> f(1)
[1]
>>> f(2)
[1, 2]
>>>

Definitely not expected.   Thanks for clarifying this.   You'll not catch me
using mutable default parameters again.

- Matt





More information about the Python-list mailing list