
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 Oliver

Oliver Walczak wrote:
The output is exactly what I expected: c1, before: d1= {'key': 'value'} d2= {'key': 'value'} c1, after: d1= {'key': 'value2'} d2= {'key': 'value2'} c2: d1= {'key': 'value2'} d2= {'key': 'value'} Exactly what do you believe is mysterious here? c2 sees the modified version of the class variable (d1), and sees a fresh copy of the instance variable (d2). Please make you question more explicit. Regards, Nick. -- Nick Coghlan | Brisbane, Australia Email: ncoghlan@email.com | Mobile: +61 409 573 268

Oliver Walczak wrote:
The output is exactly what I expected: c1, before: d1= {'key': 'value'} d2= {'key': 'value'} c1, after: d1= {'key': 'value2'} d2= {'key': 'value2'} c2: d1= {'key': 'value2'} d2= {'key': 'value'} Exactly what do you believe is mysterious here? c2 sees the modified version of the class variable (d1), and sees a fresh copy of the instance variable (d2). Please make you question more explicit. Regards, Nick. -- Nick Coghlan | Brisbane, Australia Email: ncoghlan@email.com | Mobile: +61 409 573 268
participants (2)
-
Nick Coghlan
-
Oliver Walczak