|
|
|
|
|
by nbadg
3826 days ago
|
|
Are you referring persistence of mutable default args? Which then potentially leads to bugs when you treat it as a new variable on each function call? This is intentional behavior. It's the result of one-time evaluation of default args, which is important for memoization. It's also really useful in combination with late-binding closures. For example, using a lambda as a generator function: def create_multipliers():
return [lambda x : i * x for i in range(5)]
This doesn't work, you'll get all 8's. Instead you need to: def create_multipliers():
return [lambda x, i=i : i * x for i in range(5)]
Late-binding closures and memoization strategies are pretty core language features; I wouldn't expect them to change (and many python devs would be pretty pissed if they did). Yes, this can be confusing with mutable default objects, but the alternative is to have disparate behavior depending on the mutability of the defaults, which would be an utter catastrophe.http://docs.python-guide.org/en/latest/writing/gotchas/ |
|