Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > print attitude

Reply
Thread Tools

print attitude

 
 
Batista, Facundo
Guest
Posts: n/a
 
      07-04-2003
One detail I found, don't now if it's ok:

Python 2.1.1 (#4, Mar 8 2002, 12:32:24)
[GCC 2.95.3 20010315 (release)] on sunos5

>>> a = ''
>>> a

'\xf1'
>>> print a


>>> l = ['']
>>> l

['\xf1']
>>> print l

['\xf1']
>>>


Is this OK?

Why this different behaviour?

Thank you!

.. Facundo


 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      07-05-2003
"Batista, Facundo" wrote:

> 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:

>>> class Display:

.... def __str__(self): return '(str)'
.... def __repr__(self): return '(repr)'
....
>>> d = Display()
>>> str(d)

'(str)'
>>> repr(d)

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

[' ', '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.)

--
Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ But since when can wounded eyes see / If we weren't who we were
\__/ Joi
 
Reply With Quote
 
 
 
 
Donn Cave
Guest
Posts: n/a
 
      07-07-2003
In article <(E-Mail Removed)>,
Erik Max Francis <(E-Mail Removed)> 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, (E-Mail Removed)
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      07-07-2003
Donn Cave wrote:

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

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ We're here to preserve democracy, not to practice it.
\__/ Capt. Frank Rasmey
 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      07-07-2003
In article <(E-Mail Removed)>,
Erik Max Francis <(E-Mail Removed)> wrote:

> Donn Cave wrote:
>
> > 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=e...uw1i4nqpl33h.1
l3wpteil4jb0.dlg%4040tude.net&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%
3DUTF-8%26selm%3Duw1i4nqpl33h.1l3wpteil4jb0.dlg%254040tu de.net

Donn Cave, (E-Mail Removed)
 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      07-08-2003
In article <(E-Mail Removed)>,
Steven Taschuk <(E-Mail Removed)> wrote:

> 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 Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
really interesting... or really dull. (depends on your attitude) TrevorBoydSmith@gmail.com Java 2 09-01-2006 04:38 PM
Re: Attitude Pedro Simoes MCSE 0 11-24-2005 08:01 PM
Attitude TechGeekPro MCSE 3 11-15-2005 06:09 PM
attitude of internet cafes to user's own disks Bert Computer Support 1 01-16-2004 04:52 AM
Universal "Sit back and Watch" attitude from consumers Stan Jensen DVD Video 0 09-26-2003 03:18 PM



Advertisments