list as paremeters...

Alex Martelli aleax at aleax.it
Sun Aug 31 15:19:17 CEST 2003


Ulrich Petri wrote:
   ...
> IIRC Default values are not created each time the function called but
> rather only the first time (am i correct here?). And since a list is

Almost, but not quite: they're created when the function is DEFINED,
*not* the first time it's called.  For example:

>>> x=3
>>> def f(d=x*'u'): return d
...
>>> x=8
>>> print f()
uuu

The fact that global variable x is worth 8 by the time f is first called
is irrelevant: what governs is that x was worth 3 when the def statement
was executed -- THAT instant is the one in which the default value of d
is termined and stored in the function-object f... at creation time for the
function-object f, i.e. execution time for the def statement.

> mutable it "rememberes" the previous value.

Yes, this is correct: if the default value of an argument is mutable, it
"remembers" whatever mutations you choose to perform on it.


Alex





More information about the Python-list mailing list