returning NotImplemented

Ethan Furman ethan at stoneleaf.us
Tue May 31 20:30:18 EDT 2011


Eric Snow wrote:
> Guido indicates earlier in the thread that NotImplemented is used so 
> that you know that it came from the function that you directly called, 
> and not from another call inside that function.  Why does it matter if 
> it came directly from the function or not?  And couldn't the 
> NotImplemented still have come from a call inside the operator, rather 
> than directly?

An exception can bubble up from several levels below the original call. 
  Returning NotImplemented (even if returned from another internal 
function) still has to be deliberately done by the original method.

Later in the thread it is also stated that while exception handling 
performance is not too different in C code, it is very much more 
expensive in Python code.

As to why it would matter if whether the return was from the directly 
called method vs some nested method... not sure, but I would hazard a 
guess that by returning NotImplented the interpreter knows everything 
worked as it should have (assuming no bugs, of course); whereas an 
exception could easily _not_ have come from the directly called method. 
  Imagine if the methods raised exceptions to signal normal behavior, 
and some object was buggy -- for __eq__ we'd end up with a False result, 
instead of getting the exception propagated. (!)

~Ethan~



More information about the Python-list mailing list