473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

print attitude

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
Jul 18 '05 #1
5 2053
"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 && ma*@alcyone.com && 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
Jul 18 '05 #2
In article <3F***************@alcyone.com>,
Erik Max Francis <ma*@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, do**@u.washington.edu
Jul 18 '05 #3
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 && ma*@alcyone.com && 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
Jul 18 '05 #4
In article <3F***************@alcyone.com>,
Erik Max Francis <ma*@alcyone.com> 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, do**@u.washington.edu
Jul 18 '05 #5
In article <ma**********************************@python.org >,
Steven Taschuk <st******@telusplanet.net> 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, do**@u.washington.edu
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
21
by: Steel | last post by:
Hi at all, I have a very long html page with many photo. Therefore the best to print this page is to print the some page as PDF. Therefore I maked a PDF file like my page to print best. I'ld want...
7
by: guy | last post by:
Stirring up trouble here;) why is it that C# programmers try and denigrate VB.NET while VB.NET developers seem to have no problem with C# but just prefer VB.NET? I use both and this generally seems...
9
by: Rhino | last post by:
How hard (and desireable) would it be to give the user certain choices when it comes to printing web pages? The pages on my site use colours and pictures and contain an imbedded menu, among...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
21
by: tradmusic.com | last post by:
Hi, We have the following in our stylesheet, which acts as a page header: #homepageheader { left: 0px; top: 0px; right: 0px; height: 183px; background-image:...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
36
by: Kapteyn's Star | last post by:
hi group, i try to compile code below but my compiler is failing. it tells:- printbin.c: In function ‘main’: printbin.c:9: error: invalid operands to binary & i am not able to understand what...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.