Why shouldn't you use an empty list as a default argument?

Default values for function arguments are evaluated only once when the function is defined, not each time it's called. Therefore, if you use a mutable data type (like a list) as a default argument, any modifications to this value will persist across subsequent calls to the function, leading to unexpected behavior.

An empty list is mutable, so using it as a default argument is not recommended. Instead, use None as the default value and create a new list inside the function if needed.

Comments (0)

Leave a comment