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??
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.
On Sun, 31 May 2026 08:28:46 -0000 (UTC), Veek M wrote:
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??
Just tried your code in a Jupyter notebook under Python 3.13.12, and
this was the output:
in __bool__
true -----
in __hash__
in __hash__
in __eq__ <__main__.MyClass object at 0x7f68a86a4410>
{<__main__.MyClass object at 0x7f68a8691400>}
-----
in __hash__
in __hash__
in __eq__ <__main__.MyClass object at 0x7f68a86a4410>
{<__main__.MyClass object at 0x7f68a8691400>: '20'}
As you expected, only a single element in the set, and only a single
entry in the dict.
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.
2. When is __eq__ used? If I have two instances and want to make them
equal - what's the use case?
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,123 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 36:50:38 |
| Calls: | 14,371 |
| Files: | 186,380 |
| D/L today: |
2,882 files (805M bytes) |
| Messages: | 2,540,655 |