[issue21739] Add hint about expression in list comprehensions (https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions)

New submission from Karl Richter: 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. ---------- assignee: docs@python components: Documentation messages: 220374 nosy: docs@python, krichter priority: normal severity: normal status: open title: Add hint about expression in list comprehensions (https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) type: enhancement versions: Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: In 3.x a list comprehension (like a generator expression in 2.x) *is* a separate block:
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. ---------- nosy: +r.david.murray versions: +Python 3.4, Python 3.5 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Raymond Hettinger added the comment: 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. ---------- assignee: docs@python -> rhettinger nosy: +rhettinger priority: normal -> low type: enhancement -> versions: -Python 3.4, Python 3.5 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: 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. ---------- versions: +Python 3.4, Python 3.5 -Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: rhettinger -> r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Ezio Melotti added the comment: If we don't want to go into the details of why it's not equivalent, using "roughly equivalent" might be enough. ---------- keywords: +easy nosy: +ezio.melotti stage: -> needs patch type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Rose Ames added the comment: Fwiw, I've seen a beginner be confused by this. Patch attached. ---------- keywords: +patch nosy: +superluser Added file: http://bugs.python.org/file36593/reword.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: 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. ---------- stage: needs patch -> commit review _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Roundup Robot added the comment: 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 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: 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. ---------- resolution: -> fixed stage: commit review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: In 3.x a list comprehension (like a generator expression in 2.x) *is* a separate block:
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. ---------- nosy: +r.david.murray versions: +Python 3.4, Python 3.5 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Raymond Hettinger added the comment: 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. ---------- assignee: docs@python -> rhettinger nosy: +rhettinger priority: normal -> low type: enhancement -> versions: -Python 3.4, Python 3.5 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: 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. ---------- versions: +Python 3.4, Python 3.5 -Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: rhettinger -> r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Ezio Melotti added the comment: If we don't want to go into the details of why it's not equivalent, using "roughly equivalent" might be enough. ---------- keywords: +easy nosy: +ezio.melotti stage: -> needs patch type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Rose Ames added the comment: Fwiw, I've seen a beginner be confused by this. Patch attached. ---------- keywords: +patch nosy: +superluser Added file: http://bugs.python.org/file36593/reword.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: 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. ---------- stage: needs patch -> commit review _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

Roundup Robot added the comment: 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 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________

R. David Murray added the comment: 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. ---------- resolution: -> fixed stage: commit review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21739> _______________________________________
participants (6)
-
Ezio Melotti
-
Karl Richter
-
R. David Murray
-
Raymond Hettinger
-
Rose Ames
-
Roundup Robot