Python Function Definition default parameter must point to immutable object, python Function
Why do python Function Definition default parameters must point to immutable objects?
The following is my personal understanding. If you have any mistakes, please discuss them.
Cause Analysis: when defining a function, it allocates the memory space of the function and the default parameter L pointing to the object.
1. If L points to a mutable object and the variable object is updated in the function, the variable object created when the function definition is updated when the function is called multiple times,
This variable object will not be destroyed as the function call ends, because the function definition still exists.
2. If L points to an immutable object, L is also updated in the function, and L points to the memory space of the newly created object, and the immutable object during Function Definition
Not changed. When the function call ends, the memory space of the newly created object is destroyed. It is created only when the function is called.