correlation between seemingly independent instances of same class

nikipore at googlemail.com nikipore at googlemail.com
Wed Feb 20 14:04:26 EST 2008


Hello,

the results of the following code boggle me. i would have expected
that y.data is not affected by setting z.data.

Regards Jan


--code---------------------------------------------
class Test:
  def __init__(self, x={}):
    self.data = x
  #end def
#end class Test

y = Test()
z = Test()

print 'y.data = ',y.data
print 'z.data = ',z.data

z.data['B'] = 2   # this statement also sets y.data
z.data= {'A': 1}  # this statement doesn't

print 'y.data = ',y.data
print 'z.data = ',z.data
--code end-------------------------------------------

--output-----------------------------------------------
>"C:\Program Files\Python25\pythonw" -u "thestrangething.py"
y.data =  {}
z.data =  {}
y.data =  {'B': 2}
z.data =  {'A': 1}



More information about the Python-list mailing list