[Python-Dev] Fwd: Change in repr of Decimal in 2.6
Karen Tracey
kmtracey at gmail.com
Sat Jul 19 05:59:49 CEST 2008
Meant to copy list on reply, sorry.
---------- Forwarded message ----------
From: Karen Tracey <kmtracey at gmail.com>
Date: Fri, Jul 18, 2008 at 11:48 PM
Subject: Re: [Python-Dev] Change in repr of Decimal in 2.6
To: Raymond Hettinger <python at rcn.com>
On Fri, Jul 18, 2008 at 11:19 PM, Raymond Hettinger <python at rcn.com> wrote:
> From: Karen Tracey
>
>> I noticed when trying out Python's 2.6b2 release that the repr of Decimal
>> has changed since 2.5. On 2.5:
>>
> ...
>
>> quotes were used whereas on 2.6b2:
>>
> ...
>
>> single quotes are used. Searching around I see this was done in r60773
>> with the log message:
>> Fix decimal repr which should have used single quotes like other reprs.
>>
>> but I can't find any discussion other than that.
>>
>
> Guido requested this change so that the Decimal repr would match the style
> used elsewhere (i.e. str(7) --> '7').
>
Thanks for the background. Can't say I ever noticed the inconsistency
myself.
>
> If it's here to stay, is there some straightforward way that I am unaware
>> of to construct tests that use Decimal repr but will work correctly on
>> Python 2.3-2.6?
>>
>
> A quick hack is to monkey patch the decimal module:
>
> >>> import decimal
> >>> decimal.Decimal.__repr__ = lambda s: 'Decimal("%s")' % str(s)
>
That is quick, but does seem hackish.
> A better fix is to amend to doctests to not rely on the __repr__ format.
> Instead of:
>
> >>> Decimal('7.1')
> Decimal("7.1")
>
> use:
>
> >>> print Decimal('7.1')
> 7.1
>
Yeah, but the testcases are not quite that simple. They're often testing
return values from functions and as much verifying that the type is correct
as the value, so I think I'd have to change stuff like:
>>> f.clean('1')
Decimal("1")
to:
>>> x = f.clean('1')
>>> print type(x), x
<class 'decimal.Decimal'> 1
right?
Thanks for the answer,
Karen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20080718/0fbbfe7e/attachment-0001.htm>
More information about the Python-Dev
mailing list