I have the following Python (3.6.0) class:
class SampleClass(object):
@staticmethod
def create(*data):
sample_instance = SampleClass()
sample_instance.add_data(*data)
return sample_instance
def __init__(self, data_map={}):
super(SampleClass, self).__init__()
self.data_map = data_map
def add_data(self, *data):
for key, value in data:
self.data_map.setdefault(key, []).append(value)
Where the static method create
is a convenience method for initializing a new instance. However, when invoking create
multiple times, the data_map
property is not recreated. For example:
sample_instance1 = SampleClass.create(('a', 1), ('b', 2), ('c', 3))
sample_instance2 = SampleClass.create()
sample_instance1.data_map == sample_instance2.data_map # True
sample_instance1.data_map is sample_instance2.data_map # True
If I change the __init__
method to this, the issue goes away:
def __init__(self):
super(SampleClass, self).__init__()
self.data_map = {}
Could someone help me understand what's going on here?