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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:35:08+00:00 2026-06-17T17:35:08+00:00

Suppose I write a class, but don’t define a __hash__ for it. Then __hash__(self)

  • 0

Suppose I write a class, but don’t define a __hash__ for it. Then __hash__(self) defaults to id(self) (self‘s memory address), according to the documentation.

However I don’t see in the documentation, how this value is being used.
So if my __hash__ was simply return 1, which would cause the hash of all instances of my class to be the same, they all get bucketed into the same underlying hash bucket (which I assume is implemented in C). However, this does not mean that the return value of __hash__ is being used as the key to bin elements in this underlying hash table.
So really, my question is: what happens to the value returned by __hash__? is it used as the key directly, or is its hash (or the result of some other computation performed on it) used as the key to the hash table?

In case it matters, I’m on python2.7

EDIT: To clarify, I’m not asking about how hash collisions are handled. In python, this seems to be done with linear chaining. Instead, I’m asking how the return value of __hash__ translates into the memory address (?) of the corresponding bucket.

  • 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-17T17:35:09+00:00Added an answer on June 17, 2026 at 5:35 pm

    Since Python’s hash tables have a size that is a power-of-two, the lower bits of the hash value determine the location in the hash table (or at least the location of the initial probe).

    The sequence of probes into a table size of n is given by:

    def gen_probes(hashvalue, n):
        'Same sequence of probes used in the current dictionary design'
        mask = n - 1
        PERTURB_SHIFT = 5
        if hashvalue < 0:
            hashvalue = -hashvalue
        i = hashvalue & mask
        yield i
        perturb = hashvalue
        while True:
            i = (5 * i + perturb + 1) & 0xFFFFFFFFFFFFFFFF
            yield i & mask
            perturb >>= PERTURB_SHIFT
    

    For example, the dictionary:

    d = {'timmy': 'red', 'barry': 'green', 'guido': 'blue'}
    

    is stored as an array of size 8 with each entry in the form (hash, key, value):

    entries = [['--', '--', '--'],
               [-8522787127447073495, 'barry', 'green'],
               ['--', '--', '--'],
               ['--', '--', '--'],
               ['--', '--', '--'],
               [-9092791511155847987, 'timmy', 'red'],
               ['--', '--', '--'],
               [-6480567542315338377, 'guido', 'blue']]
    

    The C source code for key insertion in Python’s dictionaries can be found here: http://hg.python.org/cpython/file/cd87afe18ff8/Objects/dictobject.c#l550

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

Sidebar

Related Questions

Suppose I define a class A and I don't want anyone to write an
Suppose, I write class A { }; The compiler should provide (as and when
Suppose I write a library with the following: public class Bar { /* ...
Suppose I only had the regular J2SE http libraries but wanted to write a
Suppose I write: new Meteor.Collection(foos); new Meteor.Collection(bars); Is there an API for accessing those
Suppose my attempt to write a pickle object out to disk is incomplete due
Suppose I want to write an app for Android OS that is not going
Suppose I let the user to write a condition using Javascript, the user can
Suppose, you want to write unittest s for functions like this one: def test_me(instream):
Suppose I have a text variable $$string . How can I write a boolean

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.