Closed hajimen closed 1 year ago
Any chance to simplify this example? 🙏 Right now it is very hard to understand what is going on.
For me this script often prints True
and False
as for the OP, but at times also prints True
and True
(as one would expect). This often points to an inconsitency between __eq__
and __hash__
given that this is related to dict equality.
And indeed, the implementation ColorBase
does not conform to the contract for a hashable object: Two instances can be equal but with different hashes. Replacing the __hash__
implementation by one that returns a constant value fixes the issue.
For objects that are used as dict keys:
a == b
must imply that hash(a) == hash(b)
For me this script often prints
True
andFalse
as for the OP, but at times also printsTrue
andTrue
(as one would expect). This often points to an inconsitency between__eq__
and__hash__
given that this is related to dict equality.And indeed, the implementation
ColorBase
does not conform to the contract for a hashable object: Two instances can be equal but with different hashes. Replacing the__hash__
implementation by one that returns a constant value fixes the issue.For objects that are used as dict keys:
a == b
must imply thathash(a) == hash(b)
- The hash must not change after construction (or at least not after being used as a dict key)
Thanks ronaldoussoren, but did you tried to append the line below?
print([(hash(k), hash(v)) for k, v in d1.items()] == [(hash(k), hash(v)) for k, v in d2.items()])
The line prints True
even when print(d1 == d2)
line prints False
.
Bug report
The code below might look redundunt, but I need the length to reproduce the bug.
The output is:
It is obviously wrong. Just removing the redundunt line
FONT_PATH = Path('calibri.ttf')
fixes the wrong output. Buffer overrun is highly suspicious.Environment