[Python-Dev] Constructor bug

Oliver Walczak oliver.walczak at momatec.de
Mon Nov 22 08:29:17 CET 2004

Dear List,

recently i got stuck on a strange class initialization bug. Please refer the
sample attached for a demonstration and watch what happens to d2 in the
second class instance.
This only works on dictionaries. Any other types initialized outside of
__init__ seem to work perfectly.

So, is it a bug or a feature? In my eyes this behaviour is not what the
programmer has got to expect. Who knows whats happening there and can
explain me if it makes any sense?

Best regards
-------------- next part --------------
# -*- coding: iso-8859-1 -*-

class test:
    d1 = {'key':'value'}
    def __init__(self):
        self.d2 = {'key':'value'}
    def Show(self):
        print 'd1=',self.d1
        print 'd2=',self.d2

c1 = test()
print 'c1, before:'
c1.d1['key'] = 'value2'
c1.d2['key'] = 'value2'
print 'c1, after:'

c2 = test()
print 'c2:'

More information about the Python-Dev mailing list