I have a dict subclass whose job is to dynamically add nested dict key if it not exists and do list append if append is called:
class PowerDict(dict):
def __getitem__(self, item):
try:
return dict.__getitem__(self, item)
except KeyError:
value = self[item] = type(self)()
return value
def append(self,item):
if type(self) != list:
self = list()
self.append(item)
so
a = PowerDict()
a['1']['2'] = 3
produce output:
a = {'1': {'2': 3}}
However, sometime i need to do something like this:
b = PowerDict()
b['1']['2'].append(3)
b['1']['2'].append(4)
should produce output:
b = {'1': {'2': [3, 4]}}
but above code produce output:
{'1': {'2': {}}}
What i am missing?