I am trying to write a wrapper object around the dictionary object in python like so
class ScoredList():
def __init__(self,dct={}):
self.dct = dct
list = ScoredList()
list.dct.update({1,2})
list2 = ScoredList()
list.dct.update({"hello","world"})
print list1.dct, list2.dct # they are the same but should not be!
It seems like I am unable to create a new ScoredList object, or rather, every scored list object shares the same underlying dictionary. Why is this?
class ScoredList2():
def __init__(self):
self.dct = {}
The above code for ScoredList2 works fine. But I want know how to overload the constructor properly in python.
A dictionary is a mutable object. In Python, default values are parsed when the function is created, meaning the same empty dictionary is assigned to every new object.
To solve this, simply do something like: