In the code I'm working with I have a pretty simple class that looks like this:
class SampleClass:
def __init__(self,name="Untitled",data={}):
self.name = name
self.data = data
Later on I have a function to update the name and data of this class:
def update_name(sample_class):
new_name = raw_input(enter a name: )
sample_class.name = new_name
def update_data(sample_class):
key = raw_input(enter a key: )
value = raw_input(enter a value: )
sample_class.data[key] = value
For some reason when I use the update_data function, it adds to the self.data of every object of the class SampleClass. For example if I had:
sample_class_1 = SampleClass()
sample_class_2 = SampleClass()
, and I added a key-value pair to sample_class_1 with update_data(sample_class_1), it would add that key-value pair to sample_class_2 as well.
Even further, after using this function, if I create a brand new SampleClass with a new name, even then, the newly created self.data will not be an empty dictionary, and will contain the key-value pair just inserted into sample_class_1.
How can I modify this code so that I can add key-value pairs to one single self.data dictionary at a time?