When do default parameters get their values set?

random832 at fastmail.us random832 at fastmail.us
Wed Dec 10 13:38:50 EST 2014


On Tue, Dec 9, 2014, at 21:44, Rustom Mody wrote:
> 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.

Of course, I used a lambda for this. The equivalent without would be:

def f():
    def g(x={}):
        return x
    return g

Ultimately it's no different from when any non-immutable expression is
executed.

def f: return {'x': {}}
f()['x'] is f()['x'] # False
d = f()
d['x'] is d['x'] # True



More information about the Python-list mailing list