[Python] Resetting generator object in Python

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have a generator object returned by multiple yield. Preparation to call this generator is rather time-consuming operation. That is why I want to reuse the generator several times.

    y = FunctionWithYield()
    for x in y: print(x)
    #here must be something to reset 'y'
    for x in y: print(x)


    Of course, I'm taking in mind copying content into simple list. Is there a way to reset my generator?


    See also: How to look ahead one element (peek) in a Python generator?

    Continue reading...

Compartilhe esta Página