Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / Python

NotImplemented

NotImplemented je jediný objekt, jímž může metoda operátoru odmítnout dvojici operandů.

Verze: Python 3.14; repository image, production runtime not attested

Syntaxe

return NotImplemented

NotImplemented je jediný objekt, jímž může metoda operátoru odmítnout dvojici operandů.

NotImplemented

Interpret pak může zkusit zrcadlovou operaci nebo příslušné konečné pravidlo. Návratová hodnota se liší od výjimky NotImplementedError. Od Pythonu 3.14 vyvolá bool(NotImplemented) chybu TypeError.

Levé sčítání odmítne, pravý operand operaci převezme a vrátí 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())

Příklady

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__)

Výsledek

handled
TypeError

Levé sčítání odmítne, pravý operand operaci převezme a vrátí handled.

Zdroje