str vs repr - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: str vs repr (/thread-3471.html) |
str vs repr - Skaperen - May-26-2017 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)). RE: str vs repr - Ofnuts - May-29-2017 If the intention is readability, use str() . If it is unambiguousness use repr() . See https://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python#2626364
RE: str vs repr - Skaperen - Jun-01-2017 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. RE: str vs repr - wavic - Jun-01-2017 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]: RE: str vs repr - Skaperen - Jun-01-2017 (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. RE: str vs repr - wavic - Jun-01-2017 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 | md5sumvs $ echo -n Tom | md5sumThe first one add a new line after Tom and md5sum is calculating 'Tom\n' But you know echo RE: str vs repr - Skaperen - Jun-01-2017 as long as i make the defaults be sep=' ' and end='\n' i think it should be OK.
RE: str vs repr - Skaperen - Jun-04-2017 str(chr(256)) != repr(chr(256)) 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 = reprand just use ascii() to get the escape sequence conversion i want.
RE: str vs repr - Ofnuts - Jun-04-2017 Better: if sys.version_info.major < 3: readable = repr else: readable= ascii RE: str vs repr - Skaperen - Jun-05-2017 (Jun-04-2017, 08:49 AM)Ofnuts Wrote: Better: 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. |