[Python-3000] UPDATED: PEP 3138- String representation in Python 3000

Jim Jewett jimjjewett at gmail.com
Wed May 28 03:44:54 CEST 2008


On 5/27/08, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Blake Winton wrote:

> > Seriously, I can write:
> >  >>> print 1, "1", Decimal("1")
> > and get as my output:
> > 1 1 1

>  Yes, but you've explicitly told it to print that,
>  so presumably it's what you want in that case.

>  Equally, you need to be explicit about how you want
>  a list printed.

Agreed; and that is why I consider the current behavior a bug.

If you want the type information in there, then you should use repr
instead of str.  For example, to get get the type information for [1,
"1", Decimal("1")], writing:

    print repr([1, "1", Decimal("1")])

is not such a huge problem.  On the other hand, if you do not care
about the specific types, and want to declutter the output, then
writing:

    print ("[" + ", ".join(str(e for e in [1, "1", Decimal("1")])) + "]")

is a bit more awkward in the best case -- and fails if your data
structures are not all nested to exactly the same depth.  Suddenly,
you need to rewrite the equivalent of the pprint module.

Again, what is the advantage of having str(x) be redundant to repr(x)
in the case of containers?

-jJ


More information about the Python-3000 mailing list