Python Forum
dict class override: how access parent values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dict class override: how access parent values?
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 809 Nov-17-2025, 11:47 AM
Last Post: Winfried
  Python inner classes inheritance from parent class Abedin 8 2,886 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 3,806 Jul-12-2024, 09:36 AM
Last Post: CAD79
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,887 May-13-2024, 05:57 AM
Last Post: Larz60+
  Printing out incidence values for Class Object SquderDragon 3 1,857 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  How to access values returned from inquirer cspower 6 4,432 Dec-26-2023, 09:34 PM
Last Post: cspower
  How can I access objects or widgets from one class in another class? Konstantin23 3 3,588 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  python update binary object (override delivered Object properties) pierre38 4 3,685 May-19-2022, 07:52 AM
Last Post: pierre38
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 2,429 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 5,466 Feb-07-2022, 07:29 PM
Last Post: saavedra29

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.