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.
| Author | carlj |
|---|---|
| Recipients | carlj |
| Date | 2008年07月10日.09:38:18 |
| SpamBayes Score | 0.010379242 |
| Marked as misclassified | No |
| Message-id | <1215682702.48.0.892529833903.issue3331@psf.upfronthosting.co.za> |
| In-reply-to |
| Content | |
|---|---|
Compare the following behaviors: Python 3.0a5 (r30a5:62856, May 10 2008, 10:34:28) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def f(x): ... if x > 5: raise StopIteration ... >>> [x for x in range(100) if not f(x)] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <listcomp> File "<stdin>", line 2, in f StopIteration >>> list(x for x in range(100) if not f(x)) [0, 1, 2, 3, 4, 5] One might object that the behavior of the list comprehension is identical to that of a for-loop: >>> r = [] >>> for x in range(100): ... if not f(x): ... r.append(x) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "<stdin>", line 2, in f StopIteration However, it can be argued that in Python 3 list comprehensions should be thought of as "syntatic sugar" for ``list(generator expression)`` not a for-loop with an accumulator. (This seems to be the motivation for no longer "leaking" variables from list comprehensions into their enclosing namespace.) One interesting question that this raises (for me at least) is whether the for-loop should also behave like a generator expression. Of course, the behavior of the generator expression can already be simulated by writing: >>> r = [] >>> for x in range(100): ... try: ... if f(x): ... r.append(x) ... except StopIteration: ... break ... >>> r [0, 1, 2, 3, 4, 5] This raises the question, do we need both a ``break`` statement and ``raise StopIteration``? Can the former just be made into syntatic sugar for the later? |
|
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2008年07月10日 09:38:23 | carlj | set | spambayes_score: 0.0103792 -> 0.010379242 recipients: + carlj |
| 2008年07月10日 09:38:22 | carlj | set | spambayes_score: 0.0103792 -> 0.0103792 messageid: <1215682702.48.0.892529833903.issue3331@psf.upfronthosting.co.za> |
| 2008年07月10日 09:38:21 | carlj | link | issue3331 messages |
| 2008年07月10日 09:38:19 | carlj | create | |