CKTIS / Python
object.__gt__
Il metodo implementa il confronto strettamente maggiore e può rifiutare con NotImplemented.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
left > rightIl metodo implementa il confronto strettamente maggiore e può rifiutare con NotImplemented.
object.__gt__
La controparte riflessa è __lt__ dell’operando destro, prioritario per una sottoclasse stretta. Se entrambi rifiutano viene sollevato TypeError.
7 supera 3, punteggi uguali non soddisfano > e la stringa non è supportata.
events=[]
class A:
def __gt__(self,other): events.append('A'); return False
class B(A):
def __lt__(self,other): events.append('B'); return True
assert A()>B() and events==['B']
Esempi
class Score:
def __init__(self, n): self.n=n
def __gt__(self, other):
if isinstance(other, Score): return self.n > other.n
return NotImplemented
print(Score(7) > Score(3), Score(3) > Score(3))
try: Score(1) > "1"
except TypeError as exc: print(type(exc).__name__)
Risultato
True False TypeError
7 supera 3, punteggi uguali non soddisfano > e la stringa non è supportata.