Recursive Generators in Python

我们两清 提交于 2019-11-30 09:09:35

I thought that a generator would precede as far down the recursion hole as necessary until it hit the yield statement

It will recurse fine, but to get the yielded value to propogate back outward, you need to do it explicitly - just like if it was a return, you would need to explicitly return the result of each recursion. So, instead of:

 self.get_next_probe(new_list, probes, unit_length)

You would do something like:

 for val in self.get_next_probe(new_list, probes, unit_length):
     yield val

Or if you're using Python 3.3 or newer, you can also do this:

yield from self.get_next_probe(new_list, probes, unit_length)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!