When do default parameters get their values set?
Rustom Mody
rustompmody at gmail.com
Tue Dec 9 21:44:15 EST 2014
On Wednesday, December 10, 2014 4:38:18 AM UTC+5:30, rand... at fastmail.us wrote:
> On Tue, Dec 9, 2014, at 16:18, Duncan Booth wrote:
> > The default parameters are actually evaluated when the 'def' statement is
> > executed and the function object is created from the default arguments
> > and
> > the previously compiled code block.
>
> Which means that if you execute the def statement [or lambda] more than
> once, you will get more than one instance of the default parameter.
>
> >>> def f(): return (lambda x={}: x)
> ...
> >>> f()() is f()()
> False
> >>> g = f()
> >>> g() is g()
> True
Nice example -- thanks.
Elaborates the why of this gotcha -- a def(inition) is imperative.
>From a semantic pov very clean.
>From an expectation pov always surprising.
More information about the Python-list
mailing list