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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:22:43+00:00 2026-05-25T00:22:43+00:00

I have subclass that inherits from a dict. On __getitem__ method I’d like to

  • 0

I have subclass that inherits from a dict. On __getitem__ method I’d like to check whether the key is numeric or not. If it is numeric I’d like to implement some other behavior and if it not then I’d like continue as it would normally do. Example:

class M(dict):
   ...
   def __getitem__(self, key):
      if self.isNumber(key):
          print "I am number"
      else:
          # continue the same way as it would have done
   ...

>>> x = M({"name": "Tom", "surname": "Baker", "age": "55"})
>>> print x["name"]
Tom
>>> x[0]
I am number

How can I do this?

UPDATE

I know that items in dict are hashed therefore it will not be in order, the reason behind what I ask is something else. And since I’m sure you’ll still ask why, this is the reason: The dict is an object and I will retrieve the objects related to the given object by their index. (Think as parent, child stuff)

  • 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-25T00:22:43+00:00Added an answer on May 25, 2026 at 12:22 am

    Not sure if this is what you are looking for…

    class M(dict):
    
        def __getitem__(self, key):
            try:
                temp = int(key)
                return "I am a number"
            except ValueError:
                return self.get(key, None)
    
    item = M({'one':'1', 'two':'2', '3':'three'})
    
    print item['one']
    print item[3]
    print item[0]
    

    As mentioned by @bpgergo, you can also use super.

    So the return would look like this:

        except ValueError:
            return super(M, self).__getitem__(key)
    

    The big difference here is that my way would (perhaps poorly) suppress any KeyError that was raised because you tried to access a key that does not exist.

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

Sidebar

Related Questions

I have a subclass that calls a method from a superclass. The method in
I have a custom NSWindowController subclass that loads a NIB file during initialization like
Is it possible to subclass and inherit from javascript Arrays? I'd like to have
I have a handful of view controllers that inherit from a subclass of UIViewController,
I have a class that inherits from UIView, and this class has some controls
I have a subclass of NSObject that implements an -(id)initWithRootElement:(MyElement *)e method. NSXMLDocument has
I have a form that inherits from 2 other forms. In my form, I
Using JavaScript how do I create a subclass that inherits values from the parent
I have a custom CustomMembershipUser that inherits from MembershipUser. public class ConfigMembershipUser : MembershipUser
I have a class that inherits from CCSprite called GameObject . I also have

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.