Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
str vs repr
#1
besides the difference in how they handle strings, is there anything in which str() will handle differently than repr()?  i have the idea of simulating the print() function in my columnizer class, and applying str() to each argument in the col.print() method call (so that col.print(foo) formats like print(foo)).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If the intention is readability, use str(). If it is unambiguousness use repr(). See https://stackoverflow.com/questions/1436...on#2626364
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
the intention is to make the .print() method in class columnize behave in a manner similar to how print() behaves, in terms of how it forms the line to be printed.  for example print(9) outputs '9' plus the line end character to go to the next line, so i want columnize.print(9) to "output" '9' to the current line in the current column, and go to the next line.  i am thinking that applying the str() function to each argument and doing sep_option.join() with a list of those would achieve the same effect.  the sep= and end= options would supported and the file= and flush= options would make no sense and raise an exception.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
In [1]: def say(text):
   ...:     print(text, end='')
   ...:     

In [2]: print('text')
text
                       # new line
In [3]: say('text')
text                  # no additional new line 
In [4]: 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jun-01-2017, 04:20 AM)wavic Wrote:
In [1]: def say(text):
   ...:     print(text, end='')
   ...:     

In [2]: print('text')
text
                       # new line
In [3]: say('text')
text                  # no additional new line 
In [4]: 

which means i need to implement end= and handle '\n' the same way, to do it up right.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Yes. If you need the line not to end with '\n' it could end with nothing or a dollar sign for example.
This is why one get a different result in this situation:

$ echo Tom | md5sum
vs
$ echo -n Tom | md5sum
The first one add a new line after Tom and md5sum is calculating 'Tom\n'
But you know echo
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
as long as i make the defaults be sep=' ' and end='\n' i think it should be OK.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
str(chr(256)) != repr(chr(256))  Cry

it looks like i want to use repr() in python2 and use ascii() in python3.  so i think i can do:

if sys.version_info.major < 3:
   ascii = repr
and just use ascii() to get the escape sequence conversion i want.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Better:
if sys.version_info.major < 3:
   readable = repr
else:
   readable= ascii
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#10
(Jun-04-2017, 08:49 AM)Ofnuts Wrote: Better:
if sys.version_info.major < 3:
   readable = repr
else:
   readable= ascii

i don't think i like that name. anyone have ideas for another? it should be understandable by py3 coders. if they see it in py3 code they should be able to understand what it does, in the context of actual use.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Repr() function Valentina 3 3,378 Aug-22-2019, 11:28 AM
Last Post: perfringo
  ascii() (repr() in py2) Skaperen 1 3,913 Jun-05-2017, 10:32 PM
Last Post: Ofnuts
  when repr() fails Skaperen 8 6,614 May-03-2017, 02:48 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020