rvcristiand
rvcristiand

Reputation: 452

What are inside the __eq__() method by default

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)

Upvotes: 2

Views: 295

Answers (3)

user8477049
user8477049

Reputation:

You can refer to existing question

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

Upvotes: 0

David
David

Reputation: 8318

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

Upvotes: 1

Barmar
Barmar

Reputation: 782105

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.

Upvotes: 7

Related Questions