CKTIS / Python
NotImplemented
NotImplemented è un oggetto unico che un metodo di operatore può restituire per rifiutare una coppia di operandi.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
return NotImplementedNotImplemented è un oggetto unico che un metodo di operatore può restituire per rifiutare una coppia di operandi.
NotImplemented
L’interprete può provare l’operazione riflessa o la regola finale appropriata. Il valore è distinto dall’eccezione NotImplementedError. Da Python 3.14 bool(NotImplemented) solleva TypeError.
L’addizione sinistra rifiuta e la destra interviene restituendo handled.
class No:
def __add__(self,other): return NotImplemented
a=No()
assert No.__add__(a,1) is NotImplemented
try: a+1
except TypeError: pass
else: raise AssertionError
assert not (object()==object())
Esempi
class Left:
def __add__(self, other): return NotImplemented
class Right:
def __radd__(self, other): return "handled"
print(Left() + Right())
try: bool(NotImplemented)
except TypeError as exc: print(type(exc).__name__)
Risultato
handled TypeError
L’addizione sinistra rifiuta e la destra interviene restituendo handled.