Decimal, __radd__, and custom numeric types...

Nick Coghlan ncoghlan at iinet.net.au
Tue Mar 1 13:54:21 CET 2005


Nick Coghlan wrote:
> Nick Coghlan wrote:
> 
>> a) Checking that replacing the relevant "raise TypeError" calls in 
>> Lib/Decimal.py with "return NotImplemented" gives you friendlier 
>> behaviour.
> 
> 
> It turns out this isn't really practical - there's too much code in the 
> module relying on those TypeErrors being raised.

Then again, maybe it's not so bad:

Py> class C:
...   def __add__(self, other):
...     print "OK!"
...   __radd__ = __add__
...
Py> x = decimal.Decimal()
Py> C() + x
OK!
Py> x + C()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "C:\Python24\lib\decimal.py", line 906, in __add__
     other = _convert_other(other)
   File "C:\Python24\lib\decimal.py", line 2863, in _convert_other
     raise TypeError, "You can interact Decimal only with int, long or Decimal da
ta types."
TypeError: You can interact Decimal only with int, long or Decimal data types.

Py> x = friendly_decimal.Decimal()
Py> C() + x
OK!
Py> x + C()
OK!

Cheers,
Nick.
http://boredomandlaziness.skystorm.net/misc/friendly_decimal.py

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list