<div dir="ltr">Meant to copy list on reply, sorry.<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Karen Tracey</b> <<a href="mailto:kmtracey@gmail.com">kmtracey@gmail.com</a>><br>
Date: Fri, Jul 18, 2008 at 11:48 PM<br>Subject: Re: [Python-Dev] Change in repr of Decimal in 2.6<br>To: Raymond Hettinger <<a href="mailto:python@rcn.com">python@rcn.com</a>><br><br><br><div dir="ltr"><div class="Ih2E3d">
On Fri, Jul 18, 2008 at 11:19 PM, Raymond Hettinger <<a href="mailto:python@rcn.com" target="_blank">python@rcn.com</a>> wrote:<br></div><div class="gmail_quote"><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
From: Karen Tracey <br><div>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I noticed when trying out Python's 2.6b2 release that the repr of Decimal has changed since 2.5. On 2.5:<br>
</blockquote></div>
...<div><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
quotes were used whereas on 2.6b2:<br>
</blockquote></div>
...<div><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
single quotes are used. Searching around I see this was done in r60773 with the log message:<br>
Fix decimal repr which should have used single quotes like other reprs.<br>
<br>
but I can't find any discussion other than that. <br>
</blockquote>
<br></div>
Guido requested this change so that the Decimal repr would match the style used elsewhere (i.e. str(7) --> '7').<div><br></div></blockquote></div><div><br>Thanks for the background. Can't say I ever noticed the inconsistency myself.<br>
</div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
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?<br>
</blockquote>
<br></div>
A quick hack is to monkey patch the decimal module:<br>
<br>
>>> import decimal<br>
>>> decimal.Decimal.__repr__ = lambda s: 'Decimal("%s")' % str(s)<br>
</blockquote></div><div><br>That is quick, but does seem hackish.<br><br></div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
A better fix is to amend to doctests to not rely on the __repr__ format.<br>
Instead of:<br>
<br>
>>> Decimal('7.1')<br>
Decimal("7.1")<br>
<br>
use:<br>
<br>
>>> print Decimal('7.1')<br>
7.1<br>
</blockquote></div><div><br>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:<br>
<br>>>> f.clean('1')<br>Decimal("1")<br><br>to:<br><br>>>> x = f.clean('1')<br>>>> print type(x), x<br><class 'decimal.Decimal'> 1<br> <br></div></div>right? <br>
<br>Thanks for the answer,<br>Karen<br></div>
</div><br></div>