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

  • Home
  • SEARCH
  • 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 8296769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:08:50+00:00 2026-06-08T15:08:50+00:00

I have a class, and I wish to write a __hash__() method for this

  • 0

I have a class, and I wish to write a __hash__() method for this class. The method I want to write will return the default hash of the object in some cases, and a hash of one of its attributes in some others. So, as a simple test case:

class Foo:
    def __init__(self, bar):
        self.bar = bar
    def __hash__(self):
        if self.bar < 10:
            return hash(self) # <- this line doesn't work
        else:
            return hash(self.bar)

The problem with this is that hash(self) simply calls self.__hash__(), leading to infinite recursion.

I gather that the hash of an object is based on the id() of that object, so I could rewrite return hash(self) as return id(self), or return id(self) / 16, but it seems bad form to me to recreate the default implementation in my own code.

It also occurred to me that I could rewrite it as return object.__hash__(self). This works, but seems even worse, as special methods are not intended to be called directly.

So, what I’m asking is; is there a way to use the default hash of an object without implicitly calling the __hash__() method of the class that object is an instance of?

  • 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-08T15:08:51+00:00Added an answer on June 8, 2026 at 3:08 pm

    To call parent implementation use:

    super(Foo, self).__hash__()
    

    It also occurred to me that I could rewrite it as return
    object.__hash__(self)
    . This works, but seems even worse, as special
    methods are not intended to be called directly.

    You are overriding a magic method, so it’s ok to call parent’s implementation directly.

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

Sidebar

Related Questions

I have this PHP class class myclass{ function name($val){ echo this is name method
I have a custom class, which has some string data. I wish to be
I'm trying to write a wrapper class for a protected method which I wish
I have a class that includes a std::list and wish to provide public begin()
I have written a base class from which I wish to derive several child
We have class lua. In lua class there is a method registerFunc() which is
I have written this short script (which I've stripped away some minor detail for
I have an XElement instance and I wish to write to a stream using
I wish to be able to write entries to a console application which will
I have a very large object which I wish to serialize. During the process

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.