print attitude

  • Thread starter Batista, Facundo
  • Start date
E

Erik Max Francis

Batista said:
Is this OK?

Why this different behaviour?

It's str vs. repr, and unfortunately it generates no end of confusion.

Let's make a simple object so we can clearly see the difference:
.... def __str__(self): return '(str)'
.... def __repr__(self): return '(repr)'
.... '(repr)'

So whether the str or repr of the object is called, we can tell the
difference. Now, consider:
print d (str)
d (repr)
[d] [(repr)]
print [d]
[(repr)]

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', ' ']

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.)
 
D

Donn Cave

Erik Max Francis said:
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, (e-mail address removed)
 
E

Erik Max Francis

Donn said:
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.

Personally, I think the internal difference between str and repr hits
right upon a proper difference: str is for a "reasonable"
human-readable representation, and repr is for as faithful and
informative a representation as possible. These both have their uses
and I approve of the distinction.

The confusion, in my opinion, comes from the fortuity of when str vs.
repr is used, which is easy to understand once you've been exposed to it
but is very confusing at first. Even for someone who's familiar with
the inaccuracy of floating point, you can very easily see how someone
would be confused and worried about the following code fragment:
[1.3999999999999999]
 
D

Donn Cave

Erik Max Francis said:
Donn said:
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.

Personally, I think the internal difference between str and repr hits
right upon a proper difference: str is for a "reasonable"
human-readable representation, and repr is for as faithful and
informative a representation as possible. These both have their uses
and I approve of the distinction.

The confusion, in my opinion, comes from the fortuity of when str vs.
repr is used, which is easy to understand once you've been exposed to it
but is very confusing at first. Even for someone who's familiar with
the inaccuracy of floating point, you can very easily see how someone
would be confused and worried about the following code fragment:
x = 1.4
print x 1.4
print [x]
[1.3999999999999999]

Maybe it isn't time to wade back into this, only to repeat
ad nauseum the same old discussion. My point is that there
is a way to look at str vs. repr that 1) doesn't use utterly
ambiguous phrases like "reasonable" or "human-readable", and
2) makes it more or less self-evident that list will repr its
contents. For more, see 61-message thread
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=uw1i4nqpl33h.1
l3wpteil4jb0.dlg%4040tude.net&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%
3DUTF-8%26selm%3Duw1i4nqpl33h.1l3wpteil4jb0.dlg%254040tude.net

Donn Cave, (e-mail address removed)
 
D

Donn Cave

Steven Taschuk said:
Quoth Donn Cave:
[...]
I think some time back, Steven Taschuk proposed to submit a rewrite
of the str & repr documentation, [...]

I did. See
<http://www.python.org/sf/727789>
I am not entirely satisfied with the text (suggestions welcomed);
I haven't taken the time to look at the matter again.

Well, I see I never did convince you to try to describe
the intent of str directly.

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top