problem manipulating a list belonging to a class

Diez B. Roggisch deets at nospam.web.de
Sun Nov 22 18:10:19 EST 2009


Marc Leconte schrieb:

> Dear all,
> 
> I have a problem with the following code (ubuntu 8.04, Python 2.5.2):
> 
> class Toto(object):
> 	def __init__(self, number, mylist=[]):
> 		self.number=number
> 		self.mylist=mylist
> 		pass
> 	pass
> 
> listA=Toto(number=1)
> listB=Toto(number=2)
> 
> listA.mylist.append(5)
> print "1) ", listA.mylist
> print "2) ", listB.mylist
> 
>>> 1) [5]
>>> 2) [5]
> 
> I would have expected
>>> 1) [5]
>>> 2) []

http://effbot.org/zone/default-values.htm

Diez



More information about the Python-list mailing list