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 8133211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:32:19+00:00 2026-06-06T09:32:19+00:00

I want to implement two different dictionaries with a predefined set of valid keys.

  • 0

I want to implement two different dictionaries with a predefined set of valid keys. Also, one dictionary contains the other.

class Otherdict (dict):

    _keys = ['A','B']

    def __init__(self):
        for key in self._keys:
            self[key] = None

    def populateDict(self):
        self['B'] = 10
        self['A'] = 12

class MyDict(dict):

    _keys = ['R','ED']

    def __init__(self):
        for key in self._keys:
            self[key] = None

    def __getitem__(self, key):
        if key not in self._keys:
            raise Exception("'" + key + "'" + " is not a valid key")
        dict.__getitem__(self,key)

    def __setitem__(self, key, value):
        if key not in self._keys:
            raise Exception("'" + key + "'" + " is not a valid key")
        dict.__setitem__(self,key,value)

    def populateDict(self):
        d = Otherdict()
        d.populateDict()
        self['R'] = 3
        self['ED'] = d


a = MyDict()
a.populateDict()
print a['ED'].__class__    #prints <type 'NoneType'>

The problem is that for some reason I cannot access the dictionary located under the ‘ED’ key. What am I doing wrong here?

I’ve also noticed that if I remove the __getitem__() method, the code works properly

  • 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-06-06T09:32:20+00:00Added an answer on June 6, 2026 at 9:32 am

    __getitem__ must return a value:

    def __getitem__(self, key):
        if key not in self._keys:
            raise Exception("'" + key + "'" + " is not a valid key")
        return dict.__getitem__(self,key)
    

    If there is no explicit return statement, Python functions return None by default.

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

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
Basically, I want a class to be able to implement two different versions of
I want to implement a class which can be used by two classes of
I am trying to find corresponding keys in two different dictionaries. Each has about
I want to implement my app in both landscape and portrait with two different
I want to implement a view having two layers on it. Then i want
I want to implement a scenario where two endpoints can securely communicate with each
I want to implement a multithread system (two threads will do) and I followed
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I have a database where I store two different kinds of data. One table

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.