The NotImplemented
value you're seeing returned from your inherited __eq__
method is a special builtin value used as a sentinel in Python. It can be returned by __magic__
methods that implement mathematical or comparison operators to indicate that the class does not support the operator that was attempted (with the provided arguments).
This can be more useful than raising an exception, as it allows Python to fall back to other options to resolve the operator use. For instance, if you do x + y
, Python will first try to run x.__add__(y)
. If that returns NotImplemented
, it will next try the "reverse" version, y.__radd__(x)
, which may work if y
is a more sophisticated type than x
is.
In the case you're asking about, x == y
, Python first tries x.__eq__(y)
, then y.__eq__(x)
, and finally x is y
(which will always evaluate to a Boolean value). Since object.__eq__
returns NotImplemented
in all cases, your class falls back to the identity comparison when you use the real operator, but shows you the NotImplemented
sentinel when you call __eq__
directly.