I have a python class that I am developing that has a dict attribute with a list field. The list is then populated with additional dicts that default to empty dicts. What I am confused about is that somehow the previously generated dictionary is getting passed as input (I think) without explicitly being called.
A minimum example:
class Test:
def __init__(self):
self.c = {'test':[]}
def populate(self):
self.add_point()
self.add_point()
def add_point(self, d={}):
print(d)
self.c['test'].append(self.get_random(d))
def get_random(self, d={}):
d['field'] = random.uniform(0,1)
return d
t = Test()
t.populate()
t.add_point()
print(t.c)
What I expected to see is something like:
{}
{}
{}
{'test': [{'field': 0.6357007816254333}, {'field': 0.41279078670743063}, {'field': 0.822863149552643}]}
What I actually see:
{}
{'field': 0.6357007816254333}
{'field': 0.822863149552643}
{'test': [{'field': 0.41279078670743063}, {'field': 0.41279078670743063}, {'field': 0.41279078670743063}]}
I think I understand what is happening is somehow the dictionary is being persisted within the class. What I don't understand is why and how I can fix it.