Once-only evaluation of default parameter values function definitions
Michael Geary
Mike at DeleteThis.Geary.com
Tue Apr 13 12:53:29 EDT 2004
Fred Ma wrote:
> Thanks, Mike. That's alot clearer. The default value looks like it's
> a persistent object. It is similar to a static local variable in C++.
> In Example#1, that object actually got changed. In contrast, in
> Example#2, the code prevents L from remaining bound to None
> beyond the first line, so the None object never gets changed. In
> fact, my 2nd explanation was close: that there is an unnamed
> persistent object that holds the default value to be bound to L for
> invocations that don't explicity supply an argument for L. The
> place I went wrong was to say that L never gets bound to None
> again, after it has been bound to a caller supplied object on a
> previous invocation of the function.
That's just about right, Fred. More precisely, the default value is as
persistent as the function definition itself (as opposed to a particular
invocation of the function).
Shalabh's message has some more details and tips.
BTW, I'd say you're doing great for one day with the language!
-Mike
More information about the Python-list
mailing list