Coder Social home page Coder Social logo

Comments (5)

pochmann3 avatar pochmann3 commented on September 26, 2024 1
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.

sobolevn avatar sobolevn commented on September 26, 2024 1

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.

terryjreedy avatar terryjreedy commented on September 26, 2024 1

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.

sobolevn avatar sobolevn commented on September 26, 2024

@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.

sobolevn avatar sobolevn commented on September 26, 2024

wait a second :)

from cpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.