Python has a singleton called NotImplemented
.
Why would someone want to ever return NotImplemented
instead of raising the NotImplementedError
exception? Won't it just make it harder to find bugs, such as code that executes invalid methods?
Python NotImplementedError Exception occurs at runtime when client characterized base classes; conceptual techniques should raise this exception when they require inferred classes to abrogate the strategy or while the class is being created to demonstrate that the genuine usage despite everything should be included.
User-defined base classes can raise NotImplementedError to indicate that a method or behavior needs to be defined by a subclass, simulating an interface. This exception is derived from RuntimeError.
It's because __lt__()
and related comparison methods are quite commonly used indirectly in list sorts and such. Sometimes the algorithm will choose to try another way or pick a default winner. Raising an exception would break out of the sort unless caught, whereas NotImplemented
doesn't get raised and can be used in further tests.
http://jcalderone.livejournal.com/32837.html
To summarise that link:
"
NotImplemented
signals to the runtime that it should ask someone else to satisfy the operation. In the expressiona == b
, ifa.__eq__(b)
returnsNotImplemented
, then Python triesb.__eq__(a)
. Ifb
knows enough to returnTrue
orFalse
, then the expression can succeed. If it doesn't, then the runtime will fall back to the built-in behavior (which is based on identity for==
and!=
)."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With