Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102961 > unrolled thread
| Started by | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| First post | 2016-02-15 18:35 +0530 |
| Last post | 2016-02-16 17:43 +1100 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
repr( open('/etc/motd', 'rt').read() ) "Veek. M" <vek.m1234@gmail.com> - 2016-02-15 18:35 +0530
Re: repr( open('/etc/motd', 'rt').read() ) Random832 <random832@fastmail.com> - 2016-02-15 09:47 -0500
Re: repr( open('/etc/motd', 'rt').read() ) Terry Reedy <tjreedy@udel.edu> - 2016-02-15 15:03 -0500
Re: repr( open('/etc/motd', 'rt').read() ) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-16 17:43 +1100
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-02-15 18:35 +0530 |
| Subject | repr( open('/etc/motd', 'rt').read() ) |
| Message-ID | <n9sie9$91s$1@dont-email.me> |
When I do at the interpreter prompt,
repr( open('/etc/motd', 'rt').read() )
i get # 1 #:
"'\\nThe programs included with the Debian GNU/Linux system are free
software;\\nthe exact distribution terms for each program are described
in the\\nindividual files in /usr/share/doc/*/copyright.\\n\\nDebian
GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\\npermitted
by applicable law.\\n'"
whereas if i just do:
open('/etc/motd', 'rt').read()
i get # 2 #:
'\nThe programs included with the Debian GNU/Linux system are free
software;\nthe exact distribution terms for each program are described
in the\nindividual files in /usr/share/doc/*/copyright.\n\nDebian
GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\npermitted by
applicable law.\n'
---------------
With # 2 # read returns a string that the interpreter displays by
calling __str__ via print so newlines are converted to \n.
What is happening with # 1 # (repr)?
repr calls __repr__ which gives you bytes.. why does this result in \\n
[toc] | [next] | [standalone]
| From | Random832 <random832@fastmail.com> |
|---|---|
| Date | 2016-02-15 09:47 -0500 |
| Message-ID | <mailman.142.1455547651.22075.python-list@python.org> |
| In reply to | #102961 |
On Mon, Feb 15, 2016, at 08:05, Veek. M wrote: > What is happening with # 1 # (repr)? > repr calls __repr__ which gives you bytes.. why does this result in \\n When you call a function that returns a string directly in the interpreter prompt (i.e. without print), it passes the result to repr, which means in this case repr has been called twice.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2016-02-15 15:03 -0500 |
| Message-ID | <mailman.147.1455566619.22075.python-list@python.org> |
| In reply to | #102961 |
On 2/15/2016 8:05 AM, Veek. M wrote:
> When I do at the interpreter prompt,
> repr( open('/etc/motd', 'rt').read() )
> i get # 1 #:
When posting questions here or at Stackoverflow or elsewhere, it is a
really good idea to develop and post a 'minimal, complete, verifiable
example' that demonstrates the behavior in question. In this case, the
open and read calls are just noise. A string with a newline illustrates
your question without distraction.
>>> s = '\n'
>>> len(s)
1
>>> len(str(s))
1
>>> len(repr(s))
4
>>> s
'\n'
>>> str(s)
'\n'
>>> repr(s)
"'\\n'"
>>> print(s)
>>> print(str(s))
>>> print(repr(s))
'\n'
>>>
For this question, 'at the interpreter prompt' is essential, so leaving
the >>> prompt is a good idea. I did the above with 3.5.1 also in IDLE
and got exactly the same result, which should be the case.
print('start')
s='\n'
print(s)
print(str(s))
print(repr(s))
print('add repr')
print(repr(s))
print(repr(str(s)))
print(repr(repr(s)))
print('end')
duplicates the collective >>> responses seen above and demonstrates, as
Random832 said, that '>>> expr' prints repr(expr).
start
'\n'
add repr
'\n'
'\n'
"'\\n'"
end
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-02-16 17:43 +1100 |
| Message-ID | <56c2c520$0$2741$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #102961 |
On Tuesday 16 February 2016 00:05, Veek. M wrote:
> When I do at the interpreter prompt,
> repr( open('/etc/motd', 'rt').read() )
Opening and reading MOTD is a needless distraction from your actual
question. This demonstrates the issue most simply:
# at the interactive interpreter
py> s = "text\n"
py> s # evaluate the string s
'text\n'
py> repr(s)
"'text\\n'"
Evaluating the string `s` in the REPL (Read-Eval-Print Loop) displays the
repr() of the string. Contrast that to using print directly:
py> print s # note the blank line
text
py> print repr(s)
'text\n'
So when you call print on a string, the string is printed in the most
accurate way possible. When you call repr() on a string, it returns a new
string containing the Python representation of a string.
So the string:
text
plus newline (but without the indent) has a representation in Python of:
'text\n'
so repr("text\n") == "'text\n'"
If you call repr() twice, you get this string:
py> print repr(repr("text\n"))
"'text\\n'"
That should look familiar:
py> repr(s)
"'text\\n'"
So when you evaluate a string on its own, the REPL prints the repr() of the
string. So if you evaluate repr(s), you see repr(repr(s)) printed.
> With # 2 # read returns a string that the interpreter displays by
> calling __str__ via print so newlines are converted to \n.
No, that's incorrect. The REPL uses the repr() of the string. We can test
this with a class that makes the difference between __str__ and __repr__
more obvious:
py> class C(object):
... def __repr__(self):
... return "the repr"
... def __str__(self):
... return "the str"
...
py> c = C()
py> print(c)
the str
py> c
the repr
--
Steve
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web