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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:36:40+00:00 2026-06-11T23:36:40+00:00

As you know, python allows us simply override dict.__getitem__ method so we can do

  • 0

As you know, python allows us simply override dict.__getitem__ method so we can do something different in there when someone tries to retrieve any value from it.

I want to do some code when one MyDict(dict) class instance is passed to update method of another python dict instance. See below:

class MyDict(dict):
    def __getitem__(self, item):
        print "Doing some stuff here"
        return dict.__getitem__(self, item)

d1 = MyDict({'1': 1, '2': 2})
d2 = {}
# I want to have d1.__getitem__ called, but it does not work :-(
d2.update(d1)
  • 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-11T23:36:42+00:00Added an answer on June 11, 2026 at 11:36 pm

    All you need is to subclass MyDict from object and create .keys() method for it. See below:

    class MyDict(object):
        def __init__(self, items=()):
            self._dict = dict(items)
    
        def keys(self):
            return self._dict.keys()
    
        def __getitem__(self, item):
            print "Doing some stuff for item:", item
            return self._dict[item]
    
        def __setitem__(self, item, value):
            self._dict[item] = value
    
        # You can add some more dict methods
    
    d1 = MyDict({'1': 1, '2': 2})
    d2 = {}
    # Now you will see some stuff executed for each
    # value extracted from d1 while updating d2
    d2.update(d1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know python can be run on GAE what is different erlang and python
I know that Python dict s will leak when items are removed (because the
I know Jython converts Python code into Java byte code, but are there any
Taking into account that I barely know python and am simply following the hello-world
I know Python (and a bunch of other languages) and I think it might
I don't know python and I'm porting a library to C#, I've encountered the
I have to really ask this question as I donot know Python. Following are
I am a novice programmer in (Java/C++/C#) and I also know Python. I am
I am a pretty good programmer(IMO only, of course. Know Python, Java well. Tried
Does anyone know if Python's shelve module uses memory-mapped IO? Maybe that question is

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.