Message161023
PEP 289 says "the semantic definition of a list comprehension in Python 3.0 will be equivalent to list(<generator expression>). Here is a counterexample where they differ (tested in 3.2):
def five(x):
"Generator yields the object x five times."
for _ in range(5):
yield x
# If we ask five() for 10 objects in a list comprehension,
# we get an error:
>>> F = five('x')
>>> [next(F) for _ in range(10)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
# But if we ask five() for 10 objects in a list(generator expr),
# we get five objects, no error:
>>> F = five('x')
>>> list(next(F) for _ in range(10))
['x', 'x', 'x', 'x', 'x'] |
|
Date |
User |
Action |
Args |
2012-05-17 22:47:37 | Peter.Norvig | set | recipients:
+ Peter.Norvig |
2012-05-17 22:47:37 | Peter.Norvig | set | messageid: <1337294857.69.0.824992743087.issue14845@psf.upfronthosting.co.za> |
2012-05-17 22:47:37 | Peter.Norvig | link | issue14845 messages |
2012-05-17 22:47:36 | Peter.Norvig | create | |
|