New instance of a class : not reset?
MRAB
python at mrabarnett.plus.com
Thu Jan 20 21:55:16 EST 2011
On 21/01/2011 02:35, Jean-Francois wrote:
> Hi,
>
> In the following example, I don't understand why attribute 'data' is
> not reset when I get the new instance of Bag
> It works if I make a copy (data[:]) but I want to understand why
>
> Thanks
>
>
> class Bag(object):
> def __init__(self, data = []):
> self.data = data
> #self.data = data[:]
> def add(self, x):
> self.data.append(x)
>
> bag = Bag()
> print bag.data
> bag.add('toto')
> print bag.data
> bag = Bag() # new instance of Bag, all attributes clear?
> print bag.data # 'toto' is still there !!!
See:
http://effbot.org/zone/default-values.htm
More information about the Python-list
mailing list