Why do operators and methods of built-in types differ

Christian Heimes lists at cheimes.de
Sat Jan 31 10:08:34 EST 2009


andrew cooke schrieb:
>> Just a correction: according to the doc, NotImplemented is not an
>> error, but a returned value.
> 
> curious, so it is.  i wonder why there is both a special return value
> (NotIMplemented) and a related exception (NotImplementedError).  seems
> very odd to have a value...

They are different and unrelated things. The NotImplentedError is an
exception class while NotImplemented is a singleton like None. When a
method like __add__ can't handle the other object it returns
NotImplemented. It doesn't raise a NotImplementedError. The
NotImplemented singleton is an optimization. The interpreter just has to
compare the memory address of the returned value with the address of
NotImplemented. That's a super fast op in C.

Christian




More information about the Python-list mailing list