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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:11:29+00:00 2026-05-20T11:11:29+00:00

class A(): def __init__(self, data=”): self.data = data def __str__(self): return str(self.data) d =

  • 0
class A():
   def __init__(self, data=''):
       self.data = data  

   def __str__(self):
       return str(self.data)

d = {}  
elem = A()  
d[elem] = 'abc'  

elem2 = A()
print d[elem2]    # KeyError  
# actually elem2! was used not elem

how can I implement this without error?

I tried to get d[elem2] (not elem) with another instance of A() BUT with the same content.

  • 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-20T11:11:30+00:00Added an answer on May 20, 2026 at 11:11 am

    The answer is yes, you need to redefine __hash__() and __eq__():

    >>> class A(object):
    ...   def __init__(self, data=''):
    ...     self.data = data
    ...   def __eq__(self, another):
    ...     return hasattr(another, 'data') and self.data == another.data
    ...   def __hash__(self):
    ...     return hash(self.data)
    ... 
    >>> a1, a2, a3 = A('foo'), A('foo'), A('bar')
    >>> d = {a1: 'foo'}
    >>> d[a1]
    'foo'
    >>> d[a2]
    'foo'
    >>> d[a3]
    Traceback (most recent call last):
      File "", line 1, in 
    KeyError: __main__.A object at 0x927d0>
    

    As explained in another comment default implementation of __hash__ is just simple identity, so if you want to make it more sophisticated, you need to define it explicitly.

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

Sidebar

Related Questions

class A: def __init__(self, n=[0]): self.data = n a = A() print a.data[0] #print
I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension
I have a class like the following: class User: def __init__(self): self.data = []
Lets assume I had the following Model class A(Models.model): def __init__(self,data): B(a=self,data=data).save() class B(Models.model):
class MyWriter: def __init__(self, stdout): self.stdout = stdout self.dumps = [] def write(self, text):
I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
My situation is something like this: class AbstractClass: def __init__(self, property_a): self.property_a = property_a
Suppose you have something like this: class intlist: def __init__(self,l = []): self.l =
If I create a class A as follows: class A: def __init__(self): self.name =

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.