Recursive Generators in Python

前端 未结 1 1572
无人及你
无人及你 2020-12-31 14:43

I wrote a function to return a generator containing every unique combination of sub-strings a given length that contain more than n elements from a primary string.

A

相关标签:
1条回答
  • 2020-12-31 15:30

    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)
    
    0 讨论(0)
提交回复
热议问题