Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:11:14+00:00 2026-06-16T00:11:14+00:00

Possible Duplicate: How to clone a Python generator object? Suppose I have a generator

  • 0

Possible Duplicate:
How to clone a Python generator object?

Suppose I have a generator ‘stuff_to_try’, I can try them one by one but if I had a method that wanted to go through the generator and that method is recursive, I want to let each recursion get a fresh new generator that starts at the first yield rather than where the last recursion left off.

def solve(something):
    if exit_condition(something):
        return

    next_to_try = stuff_to_try.next()
    while not if_works(next_to_try):
        next_to_try = stuff_to_try.next()
    solve(do_something(something))

Of course I can define stuff_to_try inside the recursive function but is there a better way? Is there an equivalent of stuff_to_try.clone().reset() or something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:11:14+00:00Added an answer on June 16, 2026 at 12:11 am

    Define the generator in a function:

    def stuff_to_try():
        return (i for i in [stuff, to, try])
    

    Then each time you want a new generator, just call the function.

    def solve(something):
        if exit_condition(something):
            return
    
        for next_to_try in stuff_to_try():
            if_works(next_to_try):
                break
        solve(do_something(something))
    

    If I read your question correctly, what you actually want is this:

    def solve(something):
        if exit_condition(something):
            return
    
        for each in [stuff, to, try]:
            if_works(each):
                break
        solve(do_something(something))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java: how to clone ArrayList but also clone its items? I have
Possible Duplicate: What is the most efficient way to clone a JavaScript object? (Most
Possible Duplicate: What is the most efficient way to clone a JavaScript object? How
Possible Duplicate: How can I increment a date by one day in Java? public
Possible Duplicate: Is close() necessary when using iterator on a Python file object for
Possible Duplicate: comparing contents of two files using python I have a file name
Possible Duplicate: What is the most efficient way to clone a JavaScript object? Copying
Possible Duplicate: How do I clone a generic list in C#? hey i have
Possible Duplicate: What do (lambda) function closures capture in Python? I have this python
Possible Duplicate: jQuery - is it bad to have multiple $(document).ready(function() {}); Can you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.