Once-only evaluation of default parameter valuesfunctiondefinitions

Robert Brewer fumanchu at amor.org
Tue Apr 13 02:13:06 EDT 2004


[Fred Ma]
> > Hello,
> >    Example#1
> >    ---------
> >    def f(a, L=[]):
> >        L.append(a)
> >        return L
> >
> > The alternative explanation that I could think of is that L is bound
> > to the unnamed object [], and the object itself changes values to
> > reflect changes to L i.e. L is now a persistent variable, retaining
> > its value between function calls unless a value is provided for L in
> > the function call's argument list.

[Terry Reedy]
> More or less correct except that it is the object that you 
> should think of
> as persistent.  The variable L only exists during the function call.

...true, but still misleading IMO--I know you're probably trying to use
Fred's terms, but please, don't use the word "variable"--it just muddies
the waters. I'd rephrase that as:

"More or less correct except that it is the object that you should think
of as persistent. The binding of the name 'L' (to either the supplied or
default object) only exists during the function call."

Fred, you might want to read: http://effbot.org/zone/python-objects.htm


FuManChu




More information about the Python-list mailing list