CKTIS / Python
object.__eq__
Metoda jednakosti može usporediti vlastite podatke ili vratiti NotImplemented za nepodržan tip.
Verzija: Python 3.14; repository image, production runtime not attested
Sintaksa
left == rightMetoda jednakosti može usporediti vlastite podatke ili vratiti NotImplemented za nepodržan tip.
object.__eq__
Python zatim može pokušati drugi operand, a nakon dva odbijanja == uspoređuje identitet. U običnoj klasi vlastita jednakost bez definicije hash vrijednosti onemogućuje hashiranje.
Oznake AA i aa jednake su, niz nije, a hash izaziva 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()
Primjeri
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__)
Rezultat
True False TypeError
Oznake AA i aa jednake su, niz nije, a hash izaziva TypeError.