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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:21:35+00:00 2026-05-22T19:21:35+00:00

I want to create a special dictionary which uses object IDs as keys, like

  • 0

I want to create a special dictionary which uses object IDs as keys, like this:

class ObjectIdDict(dict):

    def __setitem__(self, key, value):
        super(ObjectIdDict, self).__setitem__(id(key), value)

    def __getitem__(self, key):
        super(ObjectIdDict, self).__getitem__(id(key))

But if I run the following test, I get an error:

class ObjectIdDictTest(unittest.TestCase):
    def test_get_and_set(self):
        dict_to_test = ObjectIdDict()
        class Something:
            def __init__(self):
                self.x = 1
        s = Something()
        dict_to_test[s.x] = "message"
        self.assertEqual(dict_to_test[s.x], "message")     

Error message:

AssertionError: None != 'message'                   

What is wrong here?

Background:

The reason for creating such an exotic dict is that I want to store validation errors for each field of an object and want to avoid field names as strings: domain_object.errors[domain_object.field1] otherwise field names as strings (domain_object.errors["field1"]) would be bad for refactoring and code completion.

ΤΖΩΤΖΙΟΥ:

I’m certain you don’t get anything by
using IDs. obj.field1= 1;
print(id(obj.field1)); obj.field1= 2;
print(id(obj.field1))

If I would not use IDs, the key would be the value of the variable, not its address. This would lead to errors if two fields had the same value:

def test_ordinary_dict(self):
    dict_to_test = {}
    class Something:
        def __init__(self):
            self.x = 1
            self.y = 1 # same value as self.x!
    s = Something()
    dict_to_test[s.x] = "message for x"
    dict_to_test[s.y] = "message for y"
    self.assertEqual(dict_to_test[s.x], "message for x")

    # fails because dict_to_test[s.x] == dict_to_test[1] what results in:
    # "message for y"

It is not critical that changing a variables value lead to a new address since the validation result is no longer valid after that.

  • 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-22T19:21:36+00:00Added an answer on May 22, 2026 at 7:21 pm

    __getitem__ must return the result:

    def __getitem__(self, key):
      return super(ObjectIdDict, self).__getitem__(id(key))
      #^^^^^
    

    Without a return, the implicit return value is None, and therefore oiddict[key] is None for all keys.

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

Sidebar

Related Questions

I want to create a regular expression for strings which uses special characters [
I want to create a special class for holding a lot of values like
I want to create a database which name will have special characters in it.
I want to create a apache extension for my special protocol which listens on
I want to create a custom dictionary that does not copy its keys (just
I am writing a memory-managing template class in which I want to create a
I want to create some extended Binding-Markup-Extension, which behaves just like a normal WPF-Binding
I want to create a new class that is a special type of string.
I want to create a single page such as this: http://www.mywebsite.com/special/index.html But anything in
I want to create an allocator which provides memory with the following attributes: cannot

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.