print attitude

Donn Cave donn at u.washington.edu
Mon Jul 7 12:53:22 EDT 2003


In article <3F0669B5.679FB505 at alcyone.com>,
 Erik Max Francis <max at alcyone.com> wrote:
...
> Printing an object prints its str, but simply specifying it as the value
> of an expression in the interactive interpreter prints the repr.  What
> you're seeing is that both the str _and_ the repr of builtin container
> types print the _repr_ of their elements.  I consider this something of
> a wart, but the rationalization is that it might be confusing if the str
> were used when using (say) list.__str__ since it might contain spaces or
> commas itself.
> 
> >>> [' ', 'a, b', 'c', ' ']
> [' ', 'a, b', 'c', ' ']
> 
> If that were the str of the elements, it might be confusing:
> 
> >>> '[%s]' % ', '.join(map(str, [' ', 'a, b', 'c', ' ']))
> '[ , a, b, c,  ]'
> 
> I see the reasoning, but I'm not sure the benefit of the decision
> outweighs the confusion it constantly calls.  (This, along with standard
> floating point equality questions, are probably the most frequently
> asked questions about Python.)

Speaking of floats, I believe lists containing floats are the main
case where people really resist seeing the sense in this - but it's
easier to call that a wart in float's repr, since they're as bugged
wherever they see it and in my opinion rightly so.

I think some time back, Steven Taschuk proposed to submit a rewrite
of the str & repr documentation, and if that goes well it should be
more or less explain the behavior of repr(list) without having to
mention it specifically.  Then that would make this kind of question
an opportunity to clear up a whole area of confusion.  Questions
about a thing don't immediately indicate that it's ill conceived -
I guess you have to consider how many questions there would be about
the alternative.

   Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list