Comments (5)
print(a == 'True')
print(hash(a) == hash('True'))
prints:
True
False
Your class is broken.
From the __hash__
doc:
The only required property is that objects which compare equal have the same hash value
from cpython.
I've checked your example once again and found that you have both __eq__
and __hash__
, but they break the __eq__
logic. By Python's rules:
- If objects are equal
- their hashes should be equal too
and this is not the case for your Foo
class!
class Foo:
def __init__(self, value: bool):
self.value = value
def __eq__(self, other):
return self.value == bool(other)
f = Foo(1)
print(any(f is e or f == e for e in {1, 2})) # True
print(any(f is e or f == e for e in {'a', 'b'})) # True
print(any(f is e or f == e for e in {''})) # False
For a regular class without __hash__
it works just fine.
from cpython.
It is documented that a == b should imply that hash(a) == hash(b). Python works hard to make this so for builtins.
hash(1) == hash(1.0) == hash(Decimal(1)) == hash(Fraction(1,1)) == 1
True
When so, hash(a) != hash(b) implies a != b. The doc intentionally says 'equivalent', not 'exactly equal'. The speed shortcut is an CPython implementation detail. not a language requirement. Closing.
Please ask questions about CPython detail on, for instance, https://discuss.python.org/c/users/7, where many more people are likely to see answers.
from cpython.
@ThePhar indeed! Would you like to send a PR? I think it would be something like:
For several container types such as list, tuple, or collections.deque
from cpython.
wait a second :)
from cpython.
Related Issues (20)
- Running Tests from the Local System — web-platform-tests documentation HOT 2
- Name lookup in annotation scopes in classes does not work with non-dict globals HOT 2
- support bytes/bytearray in complex() constructor HOT 6
- Python 3.13.0b1: suboptimal `pdb._post_mortem` behavior if `.pdbrc` exists HOT 6
- Improve accuracy of `ntpath.normpath()` & `ntpath.abspath()` HOT 5
- Treat Fraction as a real value in mixed arithmetic operations with complex HOT 2
- ``test_venv`` prints unnecessary information HOT 6
- Honor PyOS_InputHook in the new REPL HOT 4
- " , " Grammatical confusion! ----- , 语法的迷惑 ! HOT 5
- sysconfig values for `platstdlib` do not appear to be accurate
- [C API] Add a public Py_nullptr macro (rename private _Py_NULL) HOT 5
- [C API] Split large object.h file: create refcount.h HOT 1
- pyrepl help (pydoc): "exit" does not exit, whereas "quit" does HOT 1
- Spill the stack pointer across calls in the interpreter. HOT 2
- Utilize last character gap for two-way periodic needles
- During decompression python tries to read sys.maxsize - which fail on start with MemoryError in case of any resource memory limit set.
- Wrong `follow_symlinks` default fallback implementation HOT 5
- Error exiting the new REPL with Ctrl+Z on Windows HOT 3
- [3.12] Lambda generators cause assertion failure: `python: Objects/genobject.c:400: gen_close: Assertion `exception_handler_depth > 0' failed.` HOT 3
- Improve ``NameError`` message when non-mangled name exists in the global scope HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpython.