<分区>
Possible Duplicate:
How to clone a Python generator object?
假设我有一个生成器“stuff_to_try”,我可以一个一个地尝试它们,但是如果我有一个方法想要通过生成器并且该方法是递归的,我想让每个递归获得一个全新的生成器从第一个 yield 开始,而不是从最后一个递归停止的地方开始。
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))
当然我可以在递归函数中定义 stuff_to_try 但有更好的方法吗?是否有等同于 stuff_to_try.clone().reset() 之类的东西?