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.
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:04 | admin | set | github: 65938 |
| 2014年10月01日 01:28:51 | r.david.murray | set | status: open -> closed resolution: fixed messages: + msg228052 stage: commit review -> resolved |
| 2014年10月01日 01:27:35 | python-dev | set | nosy:
+ python-dev messages: + msg228051 |
| 2014年09月10日 20:55:10 | r.david.murray | set | messages:
+ msg226714 stage: needs patch -> commit review |
| 2014年09月10日 19:43:42 | superluser | set | files:
+ reword.patch nosy: + superluser messages: + msg226710 keywords: + patch |
| 2014年06月19日 22:06:04 | ezio.melotti | set | nosy:
+ ezio.melotti messages: + msg221038 keywords: + easy type: enhancement stage: needs patch |
| 2014年06月17日 06:28:15 | rhettinger | set | assignee: rhettinger -> r.david.murray |
| 2014年06月12日 20:12:25 | r.david.murray | set | messages:
+ msg220382 versions: + Python 3.4, Python 3.5, - Python 2.7 |
| 2014年06月12日 20:03:29 | rhettinger | set | priority: 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:51 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg220375 versions: + Python 3.4, Python 3.5 |
| 2014年06月12日 19:17:59 | krichter | create | |