Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:58:34+00:00 2026-05-30T05:58:34+00:00

Ok so i’ve build my own variable handler which has a __getitem__ function for

  • 0

Ok so i’ve build my own variable handler which has a __getitem__ function for use when accessing data via data[key], it works great except for when trying to access a link of items:

data["key"]["subkey"]


def __getitem__(self, key, **args):
    print key
    ...
    return self.dict[key]

When trying to access a subkey that doesn’t exist, Python simply returns a KeyError without printing "subkey", why is this and how can I get Python to print out what I’m actually trying to get?

I know that I’ve probably misunderstood the mechanics but is there a way to emulate a dictionary AND follow the string of data that’s being requested?
Mainly so I can dynamically log the missing variables in a dictionary flow…

This obviously works (but it’s not the native syntax that I like):

data["key:subkey"]

def __getitem__(self, key, **args):
    for slice in key.split(':'):
        print key
    ...

The goal is to emulate the following,

Works:

data = {'key' : {'subkey' : 1}}
print data["key"]["subkey"]

Will not work, but I want to catch the exception within __getitem__ and then create the missing key automatically or just log the missing subkey:

data = {'key' : {}}
print data["key"]["subkey"]

Solution:

class Var():
    def __init__(self):
        self.dict = {'test' : {}}
    def __getitem__(self, var, **args):
        print ':',var
        if var in self.dict:
            v = Var(self.dict[var])
            return v

print vHandle['test']['down']

Output:

: test

: down

None

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:58:36+00:00Added an answer on May 30, 2026 at 5:58 am

    The fact is that when Python encounters an expression such as data["key"]["subkey"], what is done internally is (data["key"])["subkey"]. That is, the first part of the expression is resolved: the retrievalof the item “key” from the object “data”. Then, Python tries do call __getitem__ on the resulting object of that expression.
    If such resulting object does not have a __getitem__method itself, there is your error.

    There are two possible workarounds there: you should either work with “tuple indexes” – like
    data["key", "subkey"](and then test on your __getitem__ method wether you got a tuple instance as the key) – or make __getitem__ return an specialized object that also features a __getitem__ method – even if all it does is to log the requested keys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.