[Python-ideas] SyntaxWarning for for/while/else without break or return?

Steven D'Aprano steve at pearwood.info
Fri Oct 9 05:00:25 CEST 2009


On Fri, 9 Oct 2009 06:09:19 am Jared Grubb wrote:
> (Just for completeness, I suppose 'yield' is a third way; but like  
> return/raise, it makes the else unnecessary)

That's incorrect. Code after a yield will (in general) be executed, when 
you call the iterator again. E.g.:

def gen(y):
    for x in [1, 2, 3]:
        yield x
        if y == 0: break
    else:
        yield 0
    yield -1

gen(1) will yield 1, 2, 3, 0, -1.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list