[Python-Dev] Tricky way of of creating a generator via a comprehension expression (original) (raw)
Chris Angelico rosuav at gmail.com
Thu Nov 23 00:23:25 EST 2017
- Previous message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Next message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Nov 23, 2017 at 3:36 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
Paul Moore wrote:
3. List comprehensions are the same as list(the equivalent generator expression). I don't think that's ever been quite true -- there have always been odd cases such as what happens if you raise StopIteration in list(generatorexpression).
You mean if the genexp leaks one? That's basically an error either way
- the genexp will raise RuntimeError, but it's still an exception.
from future import generatorstop def boom(): raise StopIteration ... _[x if x < 3 else boom() for x in range(5)]_ Traceback (most recent call last): File "", line 1, in File "", line 1, in File "", line 1, in boom StopIteration _list(x if x < 3 else boom() for x in range(5))_ Traceback (most recent call last): File "", line 1, in File "", line 1, in boom StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "", line 1, in RuntimeError: generator raised StopIteration
So that's one difference removed (mostly).
ChrisA
- Previous message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Next message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]