Bug in __init__?

Fredrik Lundh fredrik at pythonware.com
Fri Jan 18 13:11:14 EST 2008


Zbigniew Braniecki wrote:

> It's really a nice pitfall, I can hardly imagine anyone expecting this, 
> or how easily could I find this info (e.g. what query should I give to 
> google to get it without bothering people on this group)

looking things up in the documentation *before* deciding that you might
have done something that nobody's done before is often a good idea:

http://docs.python.org/tut/node6.html#SECTION006710000000000000000

     "Important warning: The default value is evaluated only once.
     This makes a difference when the default is a mutable object
     such as a list, dictionary, or instances of most classes.
     /.../"

http://docs.python.org/ref/function.html

     "Default parameter values are evaluated when the function
     definition is executed. This means that the expression is
     evaluated once, when the function is defined, and that
     that same ``pre-computed'' value is used for each call.
     This is especially important to understand when a default
     parameter is a mutable object, such as a list or a
     dictionary /.../

(to be precise, the default values are evaluated when the "def" state- 
ment is executed, in the same scope as the "def" statement itself.  if 
you execute the same "def" statement multiple times, the values are 
recalculated.)

</F>




More information about the Python-list mailing list