re: "Your defaultdict approach and the dict.get with a default specified is not really equivalent. In the defaultdict case when you encounter a non existing key it adds a new entry with that key into the dict. i.e. your dict will start growing."
europa - The dictionary is only modified if you are using a method to modify it. When you are just passively querying it, it's not impacted.
class defaultdict(dict):
def __init__(self, default=None):
dict.__init__(self)
self.default = default
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.default
mdict=defaultdict(0)
mdict['gordon']=10
mdict['tim']=20
print mdict['gordon']
print mdict['tim']
print mdict['george']
print mdict
10
20
0
{'tim': 20, 'gordon': 10}