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
  Printing out incidence values for Class Object SquderDragon 3 281 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  How to access values returned from inquirer cspower 6 783 Dec-26-2023, 09:34 PM
Last Post: cspower
  How can I access objects or widgets from one class in another class? Konstantin23 3 999 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  python update binary object (override delivered Object properties) pierre38 4 1,762 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 1,311 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,762 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Due to MDLable ID does not have get attribute how to override on this jayyu 0 2,825 Dec-20-2021, 10:38 AM
Last Post: jayyu
  How to access parent object attribute Pavel_47 2 8,723 Nov-19-2021, 09:36 PM
Last Post: deanhystad
  Access instance of a class Pavel_47 5 2,085 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  access share attributed among several class methods drSlump 0 1,058 Nov-18-2021, 03:02 PM
Last Post: drSlump

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020