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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:59:02+00:00 2026-06-07T08:59:02+00:00

I am arguing with a colleague of mine, whether all Python classes really need

  • 0

I am arguing with a colleague of mine, whether all Python classes really need to be hashable. We have this class that holds symbolic expressions (something similar to SymPy).

My argument is that since we cannot compare two expressions for equality, hashing should not be allowed. For example the expressions ‘(x)’ and ‘(1*x)’ might compare equal, whereas ‘sqrt(x*x*x)’ and ‘abs(x)*sqrt(x)’ might not. Therefore, ‘hash()’ should throw an error when called with a symbolic expression.

His argument is that you should be able to use all classes as keys in dictionaries and sets. Therefore, they must also be hashable. (I’m putting words in his mouth now, he would have explained it better.).

Who is right? Is it unpythonic or not to have classes that throw errors if you try to hash them?

  • 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-07T08:59:03+00:00Added an answer on June 7, 2026 at 8:59 am

    The problem I see here is that you’re working with two different notions of equality. If I understand your comment correctly, you’ve overridden __eq__ to return an expression combining the two arguments to ==. If said expression evaluates to True (in some sense), then the two expressions are equal; and if your expression class also implements __nonzero__ (__bool__ in Python 3) in such a way that __nonzero__ returns True iff the expression is true, then superficially it seems as though this should work fine.

    But in fact, it seems to me that the concept of equality you’ve defined is a very different concept of equality than the normal concept of equality at work in Python. A fundamental requirement for hashability is that, if two items evaluate as equal, then they should be completely interchangeable. And while two of your expression objects may evaluate as “equal,” I’m not certain they’re interchangeable! After all, 5 + 5 and 8 + 2 evaluate to the same result, but they are not identical, are they? And given these two expressions, I suspect many people would expect them to hash to two separate bins in a dictionary!

    That behavior would be difficult, however, without giving __eq__ a more conventional definition. And as the docs say, “Hashable objects which compare equal must have the same hash value.” So if __eq__ says that 5 + 5 and 8 + 2 are equal, then they must hash to the same value. That means that to make your expressions hashable as they are now, you’d have to chose a __hash__ that is able to determine a canonical form for all expressions that evaluate as equal. That sounds awfully hard to me.

    In short, if these expressions are immutable, and if you redefine __eq__ to return True iff the expressions are identical (a stronger requirement than “equal”), then there should be no problem making them hashable. On the other hand, I don’t see anything wrong with an unhashable immutable type; and I wouldn’t recommend trying to make your expressions hashable without redefining __eq__.

    So it all comes down to how badly you want to define __eq__ in an unconventional way. I guess on balance I would go with a conventional definition of __eq__, simply to avoid producing unexpected behavior. After all, special cases aren’t special enough to break the rules.

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

Sidebar

Related Questions

I have a colleague who insists that his code doesn't need comments, it's "self
I have this silly argument with a friend and need an authoritative word on
A colleague and I were arguing the compilability of writing this at global scope:
so im arguing with my friend about GetWorldTransform() . im saying that this DEVICE->GetTransform(D3DTS_WORLD,
I have this class model where, Bank is a class which is now going
I was arguing with a coworker about ESBs. I mentioned that Glassfish is an
I overheard two of my colleagues arguing about whether or not to create a
Possible Duplicate: Initialize class fields in constructor or at declaration? We are arguing about
I am arguing a bit with my teacher about if this is a factory
We have been arguing back and forth at my work place about the use

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.