raise NotImplemented
should be raise NotImplementedError
FLK-F901 19 self.limits = (1, 10)
20
21 def get_number(self, min_max):
22 raise NotImplemented 23
24 def smethod():
25 """static method-to-be"""
While returning NotImplemented
would be fine, raising it doesn't work and will cause a TypeError
because NotImplemented
is not an exception type.
NotImplemented
signals to the runtime that it should ask someone else to satisfy the operation. In the expression a == b
, if a.__eq__(b)
returns NotImplemented
, then Python tries b.__eq__(a)
. If b
knows enough to return True
or False
, then the expression can succeed. If it doesn't, the runtime will fall back to the built-in behavior (based on identity for ==
and !=
).
class MyClass:
def __lt__(self, other):
raise NotImplemented # This will raise a TypeError, as NotImplemented cannot be raised
If you want to disallow comparison with this class:
class MyClass:
def __lt__(self, other):
raise NotImplementedError # Will raise the exception
class MyClass:
def __lt__(self, other):
return NotImplemented # Will allow Python to figure out the value