[Python-ideas] Change how Generator Expressions handle StopIteration

yotam vaknin tomirendo at gmail.com
Sat Nov 1 15:56:12 CET 2014


I would like to purpose that generator expressions will not catch
StopIteration exception, if this exception did not come from the iterated
object's __next__ function specifically. So generator expressions will be
able to raise StopIteration by calculating the current value of the

Here is an example of a use-case :

def izip(*args):
    iters = [iter(obj) for obj in args]
    while True:
        yield tuple(next(it) for it in iters)

a = izip([1,2],[3,4])
print(next(a),next(a),next(a)) #Currently prints : (1, 3) (2, 4) ()
list(izip([1,2],[3,4])) #Currently never returns

Even thought this is the PEP described behaviour, I think this is an
unwanted behaviour.

I think Generator Expressions should work like List Comprehension in that

def iizip(*args):
    iters = [iter(obj) for obj in args]
    while True:
        yield tuple([next(it) for it in iters])

tuple(iizip([1,2],[3,4])) #Returns [(1, 3), (2, 4)]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141101/a845c53d/attachment.html>

More information about the Python-ideas mailing list