Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197795

Re: Python __hash__ return value - what use is it?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups comp.lang.python
Subject Re: Python __hash__ return value - what use is it?
Date 31 May 2026 09:40:07 GMT
Organization Stefan Ram
Lines 63
Expires 1 Jun 2027 11:59:58 GMT
Message-ID <hash-20260531103449@ram.dialup.fu-berlin.de> (permalink)
References <10vgrfu$1dchl$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de ppIan33PDPJOuLDfxejRggT1ULlFrbIyjpb4WTyp2CkiBY
Cancel-Lock sha1:VbRWKYTboORwu+3X4MQQ9DToErk= sha256:bAJru2L/ERrzNFRl+fWmC7WzNj2BTvMDLNDcK5pFeGw=
X-Copyright (C) Copyright 2026 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed.
X-No-Archive Yes
Archive no
X-No-Archive-Readme "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access.
X-No-Html yes
Content-Language en
Xref csiph.com comp.lang.python:197795

Show key headers only | View raw


Veek M <veekjunk@foobar.com> wrote or quoted:
>the instances into a set. However when I print the set I see individual 
>instances though I want the instances to be the treated the same .ie there 
>should be just one instance in the set because the return value from 
>__hash__ is always 1.

  I used the code below, where I added statements to print lengths,
  and all lengths printed are always "1" for me.

  Python Code

#!/usr/bin/python

class MyClass:
    def __bool__(self):
        print('in __bool__')
        return True

    def __hash__(self):
        # hash value is not a key
        print('in __hash__')
        return 1

    def __eq__(self, val):
        print('in __eq__', val)
        return True

c = MyClass()
d = MyClass()

if c:
    print("true")

print('-----')
s = set()
s.add(c); print( "len( s )= ", len( s ))
s.add(d); print( "len( s )= ", len( s ))
print(s)

print('-----')
d = { c : "10", d : "20"  }
print( "len( d )= ", len( d ))
print(d)

  Python Output (abbreviated at "...")

in __bool__
true
-----
in __hash__
len( s )=  1
in __hash__
in __eq__ <__main__.MyClass object at ...>
len( s )=  1
{<__main__.MyClass object at ...>}
-----
in __hash__
in __hash__
in __eq__ <__main__.MyClass object at ...>
len( d )=  1
{<__main__.MyClass object at ...>: '20'}

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Python __hash__ return value - what use is it? Veek M <veekjunk@foobar.com> - 2026-05-31 08:28 +0000
  Re: Python __hash__ return value - what use is it? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-31 09:25 +0000
    Re: Python __hash__ return value - what use is it? Veek M <veekjunk@foobar.com> - 2026-05-31 10:13 +0000
  Re: Python __hash__ return value - what use is it? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-05-31 09:40 +0000
  Re: Python __hash__ return value - what use is it? Jon Ribbens <jon+usenet@unequivocal.eu> - 2026-05-31 13:06 +0000

csiph-web