[Python-Dev] Decimal & returning NotImplemented (or not)

Nick Coghlan ncoghlan at iinet.net.au
Tue Mar 1 14:45:43 CET 2005

Raymond Hettinger wrote:
> Try to address this in a larger context than decimal.  The same sort of
> logic is present in sets.py and in datetime objects.

Interesting. In that case, my other suggestion was to have raising 
NotImplementedError from a special method be the equivalent of returning 
NotImplemented (which makes life much easier for a class like Decimal which has 
an internal method for doing the type conversion).

Then a class ("NotImplementedTypeError"?) that inherited from both 
NotImplementedError and TypeError could be used to fix the problem in a fairly 
backward compatible way - the PyNumber machinery would see the NotImplemented 
error, and try the other available operators before falling back to generating 
its own TypeError, while direct calls with invalid arguments would still be 
raising a subclass of TypeError, so existing code to catch TypeError from direct 
calls would continue to function.

I didn't suggest this initially, since I didn't realise Decimal wasn't the only 
class with the problem, and I'm sure messing with PyNumber_* isn't possible for 
the 2.4 series :)


Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia

More information about the Python-Dev mailing list