[Python-Dev] Bug report: empty dictionary as default class argument
Fredrik Lundh
fredrik@pythonware.com
Wed, 16 May 2001 10:18:44 +0200
Idan Sofer wrote:
>
> I have found a rather annoying bug in Python, present in both Python 1.5
> and Python 2.0.
>
> If a class has an argument with a default of an empty dictionary, then
> all instances of the same class will point to the same dictionary,
> unless the dictionary is explictly defined by the constructor.
maybe you should check the documentation (or the FAQ) before
submitting bugs?
http://www.python.org/doc/current/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: if the function modifies the object
(e.g. by appending an item to a list), the default value is in
effect modified.
Cheers /F
PS. when you do report real bugs, please use the bug tracker:
http://sourceforge.net/tracker/?group_id=5470&atid=105470
"is this a bug" questions should be sent to comp.lang.python