|
|
|
|
|
by ptx
1 day ago
|
|
Function definitions in Python are just statements that are executed. When you execute the "def" statement, the default values are evaluated. You can try it in the REPL: >>> def f(foo=print("Executing def statement!")):
... print("Executing function body.")
...
Executing def statement!
>>> f()
Executing function body.
There's a fairly straight-forward explanation of this in the documentation:
https://docs.python.org/3/reference/compound_stmts.html#func..."Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call." |
|