New instance of a class : not reset?

Jean-Francois jeanfrancois.leberre at gmail.com
Thu Jan 20 21:35:46 EST 2011


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 !!!



More information about the Python-list mailing list