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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:11:57+00:00 2026-06-09T13:11:57+00:00

In Python, I often find myself having to override equality and hashing for classes

  • 0

In Python, I often find myself having to override equality and hashing for classes where the equality should be based on a particular piece of data. I usually end up abstracting this to a superclass like this, but I was wondering if Python has anything like this built in. It seems like a common task.

class ValueType(object):
    def __init__(self, *args, **kwargs): super(ValueType, self).__init__(*args, **kwargs)
    def __eq__(self, other): return self._key() == other._key()
    def __ne__(self, other): return self._key() != other._key()
    def __hash__(self): return hash(self._key())    
  • 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-09T13:11:58+00:00Added an answer on June 9, 2026 at 1:11 pm

    The answer, as far as I know is no, (based off two years of using Python) but that’s hardly proof.

    Practically though, I’m not sure I would use a ABC like you have there. It’s probably makes more sense to use it as a mixin, letting you clearnly remove the init. Python doesn’t explicitly support mixins, but it supports multiple inheritance so it’s trivial to implement. Assuming there is agreement, you’ll want to look up Python Method Resolution Order (MRO) and make sure you order your classes right. (In this case, ValueType should be before object.)

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

Sidebar

Related Questions

Quite often, I find myself wanting a simple, dump object in Python which behaves
Often when I'm using Python I'll find myself writing list comprehensions that look something
I find myself often wanting to write Python list comprehensions like this: nearbyPoints =
I find myself writing this class often in my python code when I need
When handling exceptions in python, I find myself repeating code quite often. The basic
In my Python code I often find myself doing the following (using DB-API): yValues
I often find myself writing if / elif / else constructs in python, and
I often find myself wanting to use a 3rd party python module in my
I usually write python in emacs. I'll often want to re-evaluate my file, which
I find myself trying to convert constructor parameters to their right types very often

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.