This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients facundobatista, jjconti, mark.dickinson, rhettinger, skrah
Date 2010-01-05.17:49:21
SpamBayes Score 5.551115e-17
Marked as misclassified No
Message-id <1262713764.93.0.9242048644.issue7633@psf.upfronthosting.co.za>
In-reply-to
Content
> What about the idea of changing the implementation from:
>
>        return a.__add__(b, context=self)
>
> to
>
>         return Decimal(a+b,context=self)
> ?

I think it would be better to convert the arguments a and b to Decimal before doing the addition.  In the case of addition, it doesn't make much difference:  for integers a and b, Decimal(a+b) rounded to the current context should be the same as Decimal(a) + Decimal(b).  But for e.g., division, Decimal(a/b) is potentially different from Decimal(a)/Decimal(b).

There's also the issue that the context methods can return 'NotImplemented'.  For example:

Python 2.7a1+ (trunk:77217:77234, Jan  2 2010, 15:45:27) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import *
>>> C = getcontext()
>>> C.add(Decimal(3), 'not a number')
NotImplemented

It's possible that a TypeError would make more sense here:  NotImplemented should really only be returned by direct invocation of the double underscore magic methods (__add__, etc.).

Any patch should include tests in Lib/test/test_decimal.py, of course!
History
Date User Action Args
2010-01-05 17:49:25mark.dickinsonsetrecipients: + mark.dickinson, rhettinger, facundobatista, jjconti, skrah
2010-01-05 17:49:24mark.dickinsonsetmessageid: <1262713764.93.0.9242048644.issue7633@psf.upfronthosting.co.za>
2010-01-05 17:49:22mark.dickinsonlinkissue7633 messages
2010-01-05 17:49:22mark.dickinsoncreate