CKTIS / Python
object.__eq__
An equality method can compare its own data or return NotImplemented for an unsupported type.
Version: Python 3.14; repository image, production runtime not attested
Syntax
left == rightAn equality method can compare its own data or return NotImplemented for an unsupported type.
object.__eq__
Python may then try the other operand, falling back to identity when both decline. In an ordinary class overriding equality without defining a hash disables hashing.
Labels AA and aa compare equal, the string does not, and hashing raises TypeError.
a,b=object(),object()
assert object.__eq__(a,a) is True and object.__eq__(a,b) is NotImplemented
assert (a==b) is False
class Other:
def __eq__(self,other): return True
assert a==Other()
Examples
class Label:
def __init__(self, name): self.name = name
def __eq__(self, other):
if not isinstance(other, Label): return NotImplemented
return self.name.casefold() == other.name.casefold()
print(Label("AA") == Label("aa"), Label("AA") == "aa")
try: hash(Label("a"))
except TypeError as exc: print(type(exc).__name__)
Result
True False TypeError
Labels AA and aa compare equal, the string does not, and hashing raises TypeError.