| ▲ | zahlman 5 hours ago | |
We don't call them "arrays". It has nothing to do with whether the list is empty. It has nothing to do with lists at all. It's the behaviour of default arguments. It happens at the time that the function object is created, which is during runtime. You only notice because lists are mutable. You should already prefer not to mutate parameters, and it especially doesn't make sense to mutate a parameter that has a default value because the point of mutating parameters is that the change can be seen by the caller, but a caller that uses a default value can't see the default value. The behaviour can be used intentionally. (I would argue that it's overused intentionally; people use it to "bind" loop variables to lambdas when they should be using `functools.partial`.) If you're getting got by this, you're fundamentally expecting Python to work in a way that Pythonistas consider not to make sense. | ||
| ▲ | Revisional_Sin 3 hours ago | parent [-] | |
It's best practice to avoid mutable defaults even if you're not planning to mutate the argument. It's just slightly annoying having to work around this by defaulting to None. | ||