3 ms·
Granted, it is not completely intuitive. However, it makes sense if you know pythons module loding mechanisms: The function definition is a statement which is
by tetha 16y ago
Granted, it is not completely intuitive. However, it makes sense if you know pythons module loding mechanisms:
The function definition is a statement which is evaluated at module load time, and it registers the foo-function in the module namespace. During creating the foo-function, the default-parameters are evaluated (resulting in a single list instance), and the results of this evaluation are used as default parameters in the following. That is also why something like def foo(x = random_value()) is kind of tricky and should be documented.