2

I read this (from here):

User-defined classes have __eq__() and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y).

And I wish to know if the __eq__() method by default is defined like:

def __eq__(self, other):
    return hash(self) == hash(other)
rvcristiand
  • 442
  • 7
  • 19
  • That'd only work for things that are `hash`able then. How will you check for unhashable things ? :) – han solo Sep 12 '19 at 05:04

3 Answers3

7

No, it's more like:

def __eq__(self, other)
    return self is other

You can't use hash() because it's possible for different objects to have the same hash value.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can read the following reference: https://eev.ee/blog/2012/03/24/python-faq-equality/

in the default method where you just try to compare 2 objects while not override the eq it will see if they are the same 2 objects, more like the following:

def __eq__(self, other)
    return self is other
David
  • 8,113
  • 2
  • 17
  • 36
0

You can refer to existing question

It explains how to use __hash__() with __eq__() correctly