What is eq__ in Python

How is __eq__ handled in Python and in what order?

I'm writing an updated answer for Python 3 to this question.

How is it handled in Python and in what order?

It is commonly understood, but not always the case, that calls or.

The order of evaluation is explicitly:

  1. If the type is a strict subclass (not the same type) of the type and has one, call it and return the value when the comparison is implemented.
  2. if else, has to call it, and return it when the comparison is made,
  3. Otherwise, see if we haven't called b's and has it, and call it and return it when the comparison is implemented.
  4. Otherwise, do the comparison for the identity, the same comparison as.

We know if no comparison is implemented when the method is returned.

(A method was searched for in Python 2, but it has been deprecated and removed in Python 3.)

Let's test the behavior of the first exam for ourselves by leaving B subclass A, which shows that the accepted answer is wrong in this regard:

which is only printed before the return.

How do we know this complete algorithm?

The other answers here seem incomplete and out of date so I'll update the information and Show you how to look this up yourself.

This is dealt with at the C level.

We need to look at two different bits of code here - the standard for class objects and the code that looks up and calls the method, whether it uses the standard or custom code.

default

Looking up in the corresponding C API documentation shows us that bypassed - which is defined in the type definition in in for.

So when we return, we return the object. This is the standard behavior for any subclass of objects that does not implement its own method.

How is called

Then we find the C API documents, the PyObject_RichCompare function that calls.

Then we see that the function created for the C definition is called by. So let's take a closer look at this.

The first check in this function concerns the conditions under which the objects are compared:

  • are Not the same guy but
  • The type of the second is a subclass of the type of the first and
  • The type of the second has a method:

Then call the other's method with the arguments swapped and return the value if implemented. If this method is not implemented, let's move on ...

Next, let's see if we can look up and call the method from the first type. As long as the result is not NotImplemented, ie implemented, we return it.

Otherwise, if we haven't tried the other type's method and it exists, we'll try. When the comparison is implemented, we return it.

Finally, we get a fallback in the event that it is not implemented for either type.

The fallback checks the identity of the object, that is, whether it is the same object in the same location in memory. This is the same test as for:

Conclusion

In a comparison, we first respect the comparison's subclass implementation.

Then we try to compare it with the implementation of the first object, then with that of the second object if it was not called.

Finally, we use an identity test to compare for equality.