3 ms·
My suggested workaround has nothing to do with mutable default parameters (the "items = []" code). It is true that a default parameter value is evaluated at "d
by rzzzwilson 2y ago
My suggested workaround has nothing to do with mutable default parameters (the "items = []" code). It is true that a default parameter value is evaluated at "def" evaluation time, but that's how python works for all default parameter values. It's a trap only if that default value is mutable and the programmer thinks that the default value is created at function call time.
Your code examples, though I agree with what you are saying, aren't really anything to do with what I posted. The effect of what I wrote is to bind the parameter to the value of the argument at "def" evaluation time, which is what most people want. The advice "Don't do this!" is fine for beginners, but you need further explanation: with more experience a programmer might actually use the mutable default parameter behaviour to advantage.