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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:40:25+00:00 2026-06-11T15:40:25+00:00

There are several standard ways to make a class hashable, for example (borrowing from

  • 0

There are several standard ways to make a class hashable, for example (borrowing from SO):

# assume X has 2 attributes: attr_a and attr_b
class X:
  def __key(self):
    return (self.attr_a, self.attr_b)

  def __eq__(x, y):
    return isinstance(y, x.__class__) and x.__key() == y.__key()

  def __hash__(self):
    return hash(self.__key())

Now suppose I have many classes that I want to make hashable. They are all immutable, with immutable attributes, and hashing all these attributes in bulk is acceptable (for a class with too many attributes, we would only want to hash a few attributes that are enough to avoid most collisions). Can I avoid writing __key() method by hand for every class?

Would it be a good idea to make a base class that defines __key(), __eq__, and __hash__ for them? In particular, I’m not sure whether finding all the instance attributes that should go into __hash__ is doable. I know this is generally impossible, but in this case we can assume more about the object (e.g., it’s immutable – after __init__ is finished, its attributes are all hashable, etc.).

(If the inheritance hierarchy won’t work, perhaps a decorator would?)

  • 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-11T15:40:26+00:00Added an answer on June 11, 2026 at 3:40 pm

    Instances store their attributes in self.__dict__:

    >>> class Foo(object):
    ...     def __init__(self, foo='bar', spam='eggs'):
    ...         self.foo = foo
    ...         self.spam = spam
    ... 
    >>> f = Foo()
    >>> f.__dict__
    {'foo': 'bar', 'spam': 'eggs'}
    

    Provided you don’t store any methods on your instances, a default .__key() could be:

    def __key(self):
        return tuple(v for k, v in sorted(self.__dict__.items()))
    

    where we sort the items by attribute name; the tuple() call ensures we return an immutable sequence suitable for the hash() call.

    For more complex setups you’d have to either test for the types returned by values() (skip functions and such) or use a specific pattern of attributes or repurpose __slots__ to list the appropriate attributes you can use.

    Together with your __hash__ and __eq__ methods, that’d make a good base class to inherit from for all your immutable classes.

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

Sidebar

Related Questions

There are several ways google throws at me for checking if a file is
There are several different ways I can initialize complex objects (with injected dependencies and
One of the things about programming is that there are several ways to achieve
There are several places in BCL where one can make use of IEqualityComparer .
There are several color representations in computer science : the standard RGB, but also
There are several threads on this here at SO but I didn't find one
There are several other questions about this topic that I have gone through, but
There are several variant questions on this on SO, but I did not find
There are several places on the internet that talk about having multy key dictionary
There are several good Javascript editors for Markdown / Textile (e.g.: http://attacklab.net/showdown/ , the

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.