This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add hint about expression in list comprehensions (https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions)
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: r.david.murray Nosy List: docs@python, ezio.melotti, krichter, python-dev, r.david.murray, rhettinger, superluser
Priority: low Keywords: easy, patch

Created on 2014-06-12 19:17 by krichter, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
reword.patch superluser, 2014-09-10 19:43 review
Messages (9)
msg220374 - (view) Author: Karl Richter (krichter) Date: 2014-06-12 19:17
It would be useful to have a short statement in the docs (https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) that the expression in a list comprehension isn't put into a block, but evaluated against the same block where it is located because intuitively one might think that variables can be overridden in the statement.
This applies to both 2.7 and 3.4.1.
msg220375 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-12 19:51
In 3.x a list comprehension (like a generator expression in 2.x) *is* a separate block:

>>> [x for x in range(3)]
[0, 1, 2]
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

I note that this is not in fact mentioned in the python3 version of the tutorial; there it still implies that it is completely equivalent to the unrolled if statement, which would imply it *was* in the same scope.

IMO the 2.7 tutorial is complete in the sense that the "is equivalent to" example is clearly in the same scope, while the python3 tutorial is missing a note that the comprehension is its own scope.  Given the fact that it is *different* between the two, I wonder if it would be appropriate to add a note (footnote?) to that effect to the 2.7 tutorial.  There is such a footnote in the corresponding part of the language reference.
msg220377 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-06-12 20:03
I wouldn't like to use the tutorial to highlight or draw attention a feature/bug that is going away. 

I recommend leaving the tutorial as-is.  For a decade, it has worked well for introducing people to list comprehensions.  More complete implementation specs belong in the Language Reference or in the List Comp PEP.

The tutorial is mainly about showing the normal and correct usage of tools  rather than emphasizing oddities that don't matter to most of the people, most of the time.

If you feel strongly compelled to "just add something", then consider a FAQ entry.
msg220382 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-12 20:12
OK, I have no objection to leaving the 2.7 tutorial alone.  It seems to me that the 3.x tutorial should be fixed, though, because it currently says the unrolled loop is equivalent, but it isn't.  The fact that this applies to all other comprehensions in python3 I think adds weight to the idea of including the information in the tutorial somehow.
msg221038 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-06-19 22:06
If we don't want to go into the details of why it's not equivalent, using "roughly equivalent" might be enough.
msg226710 - (view) Author: Rose Ames (superluser) * Date: 2014-09-10 19:43
Fwiw, I've seen a beginner be confused by this.  Patch attached.
msg226714 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-10 20:55
Thanks!

While this patch does sort-of go into a detail, it seems to me like it does it in a tutorial-appropriate fashion.  I'm +1 on applying this.
msg228051 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-01 01:27
New changeset 84895d037258 by R David Murray in branch '3.4':
#21739: mention subtle difference between loops and listcomps in tutorial.
https://hg.python.org/cpython/rev/84895d037258

New changeset 8279017436a2 by R David Murray in branch 'default':
Merge #21739: mention subtle difference between loops and listcomps in tutorial.
https://hg.python.org/cpython/rev/8279017436a2
msg228052 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-01 01:28
The only demure I got on this was from Ezio, who objected that the concept of 'scope' hadn't been introduced yet in the tutorial.  So I reworded it to avoid the word 'scope'.

Thanks, Rose.
History
Date User Action Args
2022-04-11 14:58:04adminsetgithub: 65938
2014-10-01 01:28:51r.david.murraysetstatus: open -> closed
resolution: fixed
messages: + msg228052

stage: commit review -> resolved
2014-10-01 01:27:35python-devsetnosy: + python-dev
messages: + msg228051
2014-09-10 20:55:10r.david.murraysetmessages: + msg226714
stage: needs patch -> commit review
2014-09-10 19:43:42superlusersetfiles: + reword.patch

nosy: + superluser
messages: + msg226710

keywords: + patch
2014-06-19 22:06:04ezio.melottisetnosy: + ezio.melotti
messages: + msg221038

keywords: + easy
type: enhancement
stage: needs patch
2014-06-17 06:28:15rhettingersetassignee: rhettinger -> r.david.murray
2014-06-12 20:12:25r.david.murraysetmessages: + msg220382
versions: + Python 3.4, Python 3.5, - Python 2.7
2014-06-12 20:03:29rhettingersetpriority: normal -> low

type: enhancement -> (no value)
assignee: docs@python -> rhettinger
versions: - Python 3.4, Python 3.5
nosy: + rhettinger

messages: + msg220377
2014-06-12 19:51:51r.david.murraysetnosy: + r.david.murray

messages: + msg220375
versions: + Python 3.4, Python 3.5
2014-06-12 19:17:59krichtercreate