unintentionaly shared attributes in two different instances

Alexandre Fayolle alf at sagittarius.logilab.fr
Thu Dec 13 15:05:40 CET 2001


On Thu, 13 Dec 2001 15:01:50 +0100, maxm <maxm at mxm.dk> wrote:
>I have this funny problem. When I make two objects from the class "Row" one
>of the attributes is shared another one is not.
>
>When I create the class like::
>
>class Row:
>
>    def __init__(self, title='', content=[]):

Don't use mutable primitives (lists, dictionaries or class instances) as
default value in a method or function. See §4.7.1 in the Python tutorial:

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

Cheers

Alexandre Fayolle
-- 
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org
Narval, the first software agent available as free software (GPL).



More information about the Python-list mailing list