classification
Title: Typo in iterator doc
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: BreamoreBoy, Susan, docs@python, ezio.melotti, python-dev, r.david.murray
Priority: normal Keywords:

Created on 2014-08-08 03:26 by Susan, last changed 2014-08-08 14:25 by ezio.melotti. This issue is now closed.

Messages (6)
msg225056 - (view) Author: Susan Tan (Susan) Date: 2014-08-08 03:26
Typo in last line: 

for line in open("myfile.txt"):
    print line,

Instead there should be no extra "," character in "print line,"
msg225057 - (view) Author: Susan Tan (Susan) Date: 2014-08-08 03:26
https://docs.python.org/2/tutorial/classes.html#iterators
msg225063 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-08-08 07:29
The comma is needed to prevent blank newlines being printed.  Please try it for yourself.
msg225071 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-08-08 13:35
The comma means print doesn't add a newline to what is printed.  This is correct because the lines read from the file already have a newline at the end.  I can see how this example becomes a little confusing in a tutorial section on iterators, but as Mark said, if you try it, you find out that it works, and if you play with removing the comma you learn things about how python works with files (I don't remember how much of that is covered earlier in the tutorial).

In python3 this is clearer, because the equivalent line uses end='', making it clearer that it is intentional and meaningful.

Except...that in the python3 tutorial it currently *doesn't* use end='', so that example is in fact wrong and needs to be fixed :)
msg225073 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-08-08 14:24
New changeset 35db84720d8d by Ezio Melotti in branch '3.4':
#22170: avoid printing newlines twice in tutorial example.
http://hg.python.org/cpython/rev/35db84720d8d

New changeset 79e469ae13b7 by Ezio Melotti in branch 'default':
#22170: merge with 3.4.
http://hg.python.org/cpython/rev/79e469ae13b7
msg225074 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-08-08 14:25
Fixed, thanks for the report!
History
Date User Action Args
2014-08-08 14:25:13ezio.melottisetstatus: open -> closed

assignee: docs@python -> ezio.melotti

nosy: + ezio.melotti
messages: + msg225074
resolution: fixed
stage: needs patch -> resolved
2014-08-08 14:24:43python-devsetnosy: + python-dev
messages: + msg225073
2014-08-08 13:35:54r.david.murraysetversions: + Python 3.4, Python 3.5, - Python 2.7
nosy: + r.david.murray

messages: + msg225071

type: enhancement -> behavior
stage: needs patch
2014-08-08 07:29:04BreamoreBoysetnosy: + BreamoreBoy
messages: + msg225063
2014-08-08 03:26:49Susansetmessages: + msg225057
2014-08-08 03:26:17Susancreate