Python Forum
dict class override: how access parent values? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: dict class override: how access parent values? (/thread-36573.html)



dict class override: how access parent values? - Andrey - Mar-06-2022

I'm try override dict class for way that compatible with standart dict class. How i can get access to parent dict attribute if i override getitem method?

class CSJSON(dict): def __getitem__(self, Key : str): Key = Key + 'zzz' # sample of key modification for app use return(super()[Key])
Then i faced error:
Error:
'super' object is not subscriptable.
If i'm use self[Key] - then i get infinite recursive call of getitem.


RE: dict class override: how access parent values? - deanhystad - Mar-06-2022

class CSJSON(dict): def __setitem__(self, key : str, value : all): return super().__setitem__(key+'zzz', value) def __getitem__(self, key : str): return super().__getitem__(key+'zzz') x = CSJSON() x['Hi'] = "Hola" x['Bye'] = 'Adios' print(*x.keys(), x['Hi'])
Output:
Hizzz Byezzz Hola



This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.