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

Michael Hudson mwh at python.net
Wed Mar 2 13:21:08 CET 2005

Nick Coghlan <ncoghlan at iinet.net.au> writes:

> Neil Schemenauer wrote:
>>  IMO, Decimal should
>> be returning NotImplemented instead of raising TypeError.
> I agree, but I'm also interested in the fact that nobody commented on
> this before Python 2.4 went out. I read the reference page on numeric
> coercion more times than I care to count, and still didn't register
> that there might be an issue with raising the TypeError - and that's
> only me. Relative to people like Raymond and Facundo, I spent
> comparatively little time working on Decimal :)
> I think the problem arose because the natural thing to do in Python
> code is to push the type inspection for operations into a common
> method, and have that method raise TypeError if the other argument
> isn't acceptable.
> Trying to convert that exception to a 'NotImplemented' return value is
> a pain (it's less of a pain in C, since you're already checking for
> error returns instead of using exceptions). 

It seems to me that the most straightforward thing would be for
_convert_other to return NotImplemented, or to split it into two
functions, one that returns NotImplemented and one that raises and use
the former in __methods__.  This would mean a certain amount of

   other = _convert_other_ni(other)
   if other is NotImplemented: return other

but this seems mild in comparisons to the contortions the module
already performs in the name of correctness.


PS: this beahviour seems odd already:

>>> decimal.getcontext().abs(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/Users/mwh/Source/python/dist/src/Lib/decimal.py", line 2321, in abs
    return a.__abs__(context=self)
TypeError: wrapper __abs__ doesn't take keyword arguments

couldn't/shouldn't the context methods do a _convert_other (of the
erroring out kind) of their arguments?

  Gullible editorial staff continues to post links to any and all
  articles that vaguely criticize Linux in any way.
         -- Reason #4 for quitting slashdot today, from

More information about the Python-Dev mailing list