Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197793
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Veek M <veekjunk@foobar.com> |
| Newsgroups | comp.lang.python |
| Subject | Python __hash__ return value - what use is it? |
| Date | Sun, 31 May 2026 08:28:46 -0000 (UTC) |
| Organization | A noiseless patient Spider |
| Lines | 45 |
| Message-ID | <10vgrfu$1dchl$1@dont-email.me> (permalink) |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Sun, 31 May 2026 08:28:47 +0000 (UTC) |
| Injection-Info | dont-email.me; logging-data="1487413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OdI3NLe8RTV6DUKibHZZl"; posting-host="19ee0fa4a702ab58bc30ce99d61c320d" |
| User-Agent | Pan/0.154 (Izium; 517acf4) |
| Cancel-Lock | sha1:dyQzp2u3BVVf388vizXNLCI8aCs= sha256:Rmf3W68c43YI9vk0tP58jtqoGwHI3CtpBnh2rn/R1VQ= sha1:hmCXF4xCjCXJsCQAOgLC2icIblw= |
| Xref | csiph.com comp.lang.python:197793 |
Show key headers only | View raw
1. So I am returning 1 for every instance of MyClass and then inserting
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.
In the dictionary when I print it I see the instances as individual keys
though I want to see a single key - what gives?? Why is the return value
of __hash__ being ignored?? (I can see it's being called)
2. When is __eq__ used? If I have two instances and want to make them
equal - what's the use case?
#!/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)
s.add(d)
print(s)
print('-----')
d = { c : "10", d : "20" }
print(d)
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll 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