class Student():
NAME = ''
DICT = {}
def __init__(self, name):
self.NAME = name
self.DICT['name'] = name
def change_DICT(self, change):
self.DICT['name'] = change
student_one = Student('Leo')
student_two = Student('Liz')
print ('Student one NAME: ' + student_one.NAME)
print ('Student two NAME: ' + student_two.NAME)
print ('---------------------------------')
print ('Student one DICT: ' + str(student_one.DICT))
print ('Student two DICT: ' + str(student_two.DICT))
print ('---------------------------------')
student_one.change_DICT('Tom')
print ('Student one DICT: ' + str(student_one.DICT))
print ('Student two DICT: ' + str(student_two.DICT))
>> Student one NAME: Leo
>> Student two NAME: Liz
>> ---------------------------------
>> Student one DICT: {'name': 'Liz'}
>> Student two DICT: {'name': 'Liz'}
>> ---------------------------------
>> Student one DICT: {'name': 'Tom'}
>> Student two DICT: {'name': 'Tom'}
I have been playing with this for hours and still can't wrap my head around it. How does changing DICT
of one instance of the class Student
changes DICT
of all other instances synchronously.
How does this happen and if I really need to use a dict
, what is the best way to work around this?