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 Peter.Norvig
Recipients Peter.Norvig
Date 2012-05-17.22:47:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1337294857.69.0.824992743087.issue14845@psf.upfronthosting.co.za>
In-reply-to
Content
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']
History
Date User Action Args
2012-05-17 22:47:37Peter.Norvigsetrecipients: + Peter.Norvig
2012-05-17 22:47:37Peter.Norvigsetmessageid: <1337294857.69.0.824992743087.issue14845@psf.upfronthosting.co.za>
2012-05-17 22:47:37Peter.Norviglinkissue14845 messages
2012-05-17 22:47:36Peter.Norvigcreate