When do default parameters get their values set?

random832 at fastmail.us random832 at fastmail.us
Tue Dec 9 18:07:53 EST 2014


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



More information about the Python-list mailing list