[issue1437] List member inside a class is shared by all instances of the class
glubglub
report at bugs.python.org
Tue Nov 13 13:53:38 CET 2007
New submission from glubglub:
In the class below, 'arr' list should be unique for each instance of
class Blah. In reality, 'arr' is shared by all instances of 'Blah'
class Blah:
arr = [] # this member should not be
# shared across all instances of blah
s = ''
def __init__(self, s):
self.s = s
def __str__( self):
return '[%s, %s]' % (self.s, str(self.arr))
objs = []
objs.append(Blah('obj-a'))
objs.append(Blah('obj-b'))
objs.append(Blah('obj-c'))
# add to first object's array
objs[0].arr.append('abc')
# bug: 'abc' got added to all arrays
# print all arrays
for obj in objs:
print obj
------------------------------
Actual Output:
[obj-a, ['abc']]
[obj-b, ['abc']]
[obj-c, ['abc']]
Expected Output:
[obj-a, ['abc']]
[obj-b, []]
[obj-c, []]
----------
components: Interpreter Core
messages: 57449
nosy: glubglub
severity: normal
status: open
title: List member inside a class is shared by all instances of the class
versions: Python 2.3, Python 2.4, Python 2.5
__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1437>
__________________________________
More information about the Python-bugs-list
mailing list