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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:06:08+00:00 2026-05-20T00:06:08+00:00

What must I do to use my objects of a custom type as keys

  • 0

What must I do to use my objects of a custom type as keys in a Python dictionary (where I don’t want the “object id” to act as the key) , e.g.

class MyThing:
    def __init__(self,name,location,length):
            self.name = name
            self.location = location
            self.length = length

I’d want to use MyThing’s as keys that are considered the same if name and location are the same.
From C#/Java I’m used to having to override and provide an equals and hashcode method, and promise not to mutate anything the hashcode depends on.

What must I do in Python to accomplish this ? Should I even ?

(In a simple case, like here, perhaps it’d be better to just place a (name,location) tuple as key – but consider I’d want the key to be an object)

  • 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-20T00:06:09+00:00Added an answer on May 20, 2026 at 12:06 am

    You need to add 2 methods, note __hash__ and __eq__:

    class MyThing:
        def __init__(self,name,location,length):
            self.name = name
            self.location = location
            self.length = length
    
        def __hash__(self):
            return hash((self.name, self.location))
    
        def __eq__(self, other):
            return (self.name, self.location) == (other.name, other.location)
    
        def __ne__(self, other):
            # Not strictly necessary, but to avoid having both x==y and x!=y
            # True at the same time
            return not(self == other)
    

    The Python dict documentation defines these requirements on key objects, i.e. they must be hashable.

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

Sidebar

Related Questions

I have a number of custom objects of type X. X has a number
I'm trying to place custom objects into a sorted dictionary... I am then trying
I have the following class: class DerivedMap extends Hashmap<String,Object> {} Which I use because
I'm trying to use .MemberwiseClone() on a custom class of mine, but it throws
Firstly, I will say what I want to compare the following: My Custom Object
If I have an NSMutableArray of custom objects, how can I must easily clear
The below code is a factory class that is delivers objects of type IGraph
When we must use DebuggerDisplay Attributes? What is the advantage of using this?
In my company I must use a Bll, Dal and model layer for creating
There is a particular website I must use for work which is absolutely heinous

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.