[New-bugs-announce] [issue35725] Using for...in.. generator-iterator

Yoong Hor Meng report at bugs.python.org
Fri Jan 11 23:26:24 EST 2019

New submission from Yoong Hor Meng <yoonghm at gmail.com>:

def f():
    print('-- Start --')
    yield 1
    print('-- Middle --')
    yield 2
    print('-- Finished --')
    yield 3

gen = f()
for x in gen:
    print('Another things ...')

The output:

-- Start --
Another things ...
-- Middle --
-- Finished --
Another things ...

I noticed that the generator function will execute whenever it is in the for...in loop.  Is it expected? I do not see it documented anywhere. Thanks.

components: Interpreter Core
messages: 333510
nosy: yoonghm
priority: normal
severity: normal
status: open
title: Using for...in.. generator-iterator
type: behavior
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list