[Python-Dev] Change in repr of Decimal in 2.6

Guido van Rossum guido at python.org
Sat Jul 19 05:55:51 CEST 2008


This is an example of the problem with doctest -- it's easy to
overspecify the tests. I don't think that whether the repr() of a
Decimal uses single or double quotes should be considered a spec cast
in stone by doctests.

On Fri, Jul 18, 2008 at 8: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').
>
>
>> 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)
>
> 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
>
>
> Raymond
>
>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/python%40rcn.com
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/guido%40python.org
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list