Changing Decimal.__pos__ and Decimal.__neg__ [Was: Re: Python Numbers as Human Concept Decimal System]
On 10 March 2014 08:32, Mark Dickinson
On Sun, Mar 9, 2014 at 8:07 PM, Oscar Benjamin
wrote: The problem though is with things like +3.14d or -3.14d. Python the language treats the + and - signs as not being part of the literal but as separate unary operators. This is unimportant for int, float and imaginary literals because there unary + is a no-op and unary - is exact. For decimal this is not the same as +Decimal('3.14') is not the same as Decimal('+3.14'):
Another option would be to change the behaviour of + and - on Decimal objects by binding `__pos__` and `__neg__` to the specification's 'copy' and 'copy-negate' operations instead of (as currently) to the 'plus' and 'minus' operations. Those behaviours are more in keeping with the way that + and - operate on other objects.
Hi Mark and thanks for responding, Do you think it's really feasible to make such a change? The use of unary + for rounding to context has been a feature of the decimal module for as long as I've been using it. It's shown in the docs both in the Recipes section and the FAQ. I just advised Mark Harris to do this and it know appears ~20 times in his pdeclib module: https://code.google.com/p/pythondecimallibrary/source/browse/pdeclib.py#207 I would expect that a change such as this would break quite a lot of code (some of mine would break) so there would have to be some kind of compatibility mechanism or deprecation process or something but I can't think of a good way of doing it. Oscar
participants (7)
-
Mark Dickinson
-
Nick Coghlan
-
Oscar Benjamin
-
random832ļ¼ fastmail.us
-
Stefan Krah
-
Steven D'Aprano
-
Tim Peters