Why is the Python function definition default parameter must point to the immutable object?
The following is a personal understanding, if there is a wrong place, welcome to discuss
Cause analysis: When defined, the function allocates the memory space of the object to which the function and default parameter L point.
1. If L is pointing to a mutable object, and the variable object is updated in the function, the variable object is created when the function is called more than once, and the update is the function definition.
This mutable object is not destroyed as the function call ends because the function definition still exists.
2. If L is pointing to an immutable object, the function also updates L, at which point L points to the memory space of the newly created object, and the immutable object when the function is defined
has not changed. At the end of the function call, the memory space of the newly created object is destroyed. Because it was created when the function was called.
Python function definition default parameter must point to immutable object