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.

classification
Title: Catching StopIteraion inside generator expression.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: terry.reedy, tomirendo
Priority: normal Keywords:

Created on 2014-10-29 21:19 by tomirendo, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg230246 - (view) Author: yotamv (tomirendo) Date: 2014-10-29 21:19
Inside a list comprehension expression, if a StopIteration exception is raised, the interpreter assumes the exception was raised by the object Iterated buy the list comprehension expression.

For example, this generator will never stop, and will keep returning empty tuples:

def izip(*args):
    iters = [iter(obj) for obj in args]
    while True:
        yield tuple(next(it) for it in iters)
x = izip([1,2],[3,4])
print(next(x)) #(1,3)
print(next(x)) #(2,4)
print(next(x)) #()
msg230419 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-11-01 01:29
(One copy of a message is enough; I unlinked the duplicate.)

I am not sure what you mean by you initial sentence, but I do not think it is correct.  In any case, "next(it) for it in iters" is a generator expression, not a list comprehension. The tuple call swallows the StopIteration, so the behavior is correct.

If you want to discuss further, please ask on python-list, also accessible at news.gmane.com either as a web page or as newsgroup gmane.comp.python.general.
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66950
2014-11-01 01:29:57terry.reedysetstatus: open -> closed

title: Catching StopIteraion inside list comprehension -> Catching StopIteraion inside generator expression.
nosy: + terry.reedy

messages: + msg230419
resolution: not a bug
stage: resolved
2014-11-01 00:50:36terry.reedysetmessages: - msg230248
2014-10-29 21:27:46tomirendosetmessages: + msg230248
2014-10-29 21:19:01tomirendocreate