Add by ZHJ: The Python document clearly explains how the default parameters work, as follows
"Default parameter values are evaluated when the function definition is executed. This means the expression was evaluated once when the function was defined, and that the same ' pre-computed ' value is Used for each call. This was especially important to understand if a default parameter is a mutable object, such as a list or a dictionary:i f The function modifies the object (e.g. by appending an item to a list), the default value was in effect modified. This is generally not a what was intended. A-the-to-use-around-A-use- None as the default, and explicitly test for it in the body of the function
def whats_on_the_telly (penguin=None): if is none: = [] penguin.append ("property of theZoo") return Penguin
"
See Https://docs.python.org/2/reference/compound_stmts.html#function-definitions
Original: Python function parameter default values of traps and principles
The answer to this question can be found on StackOverflow. The most important part of the answer that will have the most votes is the following excerpt:
Actually, this was not a design flaw, and it was not because of internals, or performance.
It comes simply from the fact that functions in Python is first-class objects, and not only a piece of code.
As soon as you get to think into the this and then it completely makes Sense:a function are an object being evaluated in its Definition Default parameters is kind of "member data" and therefore their state could change from one call to the other-exactly as In any other object.
In any case, Effbot have a very nice explanation of the reasons for this behavior in Default Parameter Values in Python.
I found it very clear, and I really suggest reading it for a better knowledge of what function objects work.
In this answer, the speaker thinks that the function is an internal-level object, considering the implementation of the Python compiler. The default value of the parameter is the property of the object. In any other language, object properties are bound when the object is created. Therefore, it is not surprising that the default value of a function parameter is bound at compile time.
However, there are many other respondents who do not buy it and think that even first-class object the way it can be used is closure bound at execution time.
This is a design flaw. It is a design decision; Perhaps a bad one and not a accident. The state thing are just like any and closure:a closure are not a function, and a function with mutable default argument is not a function.
There is even a rebuttal of the realization of the logic, simply from the design point of view: As long as it is against the procedural ape basic thinking logic behavior, are design flaws! Here are some of their arguments:
> Sorry, but anything considered ' the biggest WTF in Python ' is most definitely a design flaw. This was a source of bugs for everyone at some point, because no one expects that behavior at First-which means it should Not there are been designed that, that is, to begin with.
The phrases "This isn't generally what was intended" and "A-around this is" smell like they ' re documenting a design f Law.
Well, the answer to this question will always be a mystery, if not personally clarified by the Python author.
Python function parameter default value trap and principle drill down (GO)