[Python-Dev] PEP 3144 review.

Nick Coghlan ncoghlan at gmail.com
Wed Sep 30 02:44:07 CEST 2009


Martin v. Löwis wrote:
>> I would say that there certainly are precedents in other areas for
>> keeping the information about the input form around. For example,
>> occasionally it would be handy if parsing a hex integer returned an
>> object that was compatible with other integers but somehow kept a hint
>> that would cause printing it to use hex by default.
> 
> At the risk of bringing in false analogies: it seems that Python
> typically represents values of some type in their canonical form,
> rather than remembering the form in which they arrived in the program:
> - integer values "forget" how many preceding zeroes they have
> - string literals forget which of the characters had been escaped, and
>   whether the string was single- or double-quoted
> - floating point values forget a lot more about their literal
>   representation (including even the literal decimal value)
> 
> I guess a close case would be rational numbers: clearly, 3÷2 == 6÷4;
> would a Python library still remember (and repr) the original numerator
> and denominator?

For a concrete example of an object which remembers details about its
creation that it ignores when determining equality, we have decimal.Decimal:

.>> from decimal import Decimal as d
.>> x = d("3.0")
.>> y = d("3.00")
.>> x
d("3.0")
.>> y
d("3.00")
.>> repr(x) == repr(y)
False
.>> x.as_tuple() == y.as_tuple()
False
.>> x == y
True

(It was actually thinking of this example which led to me suggesting
that the equivalence classes of IPNetwork just needed adjusting rather
than the .ip attribute being removed completely)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list