[Python] Why does generator raise an exception when a loop that iterates over it is...

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    def gen():
    try:
    yield 1
    yield 2
    except:
    print('hi')
    def func():
    for x in gen():
    return x
    print(func())


    This code prints hi and then prints 1. Why doesn't it just print 1? What exception was raised?

    Continue reading...

Compartilhe esta Página