Python generator to yield everything from another generator call

前端 未结 4 705
星月不相逢
星月不相逢 2021-01-07 19:26

I have a Python generator that can call itself to get more elements to yield. It looks like this:

def gen(list):
    # ...
    if list:
        for x in gen         


        
相关标签:
4条回答
  • 2021-01-07 19:46

    Python 3.3 added the yield from keyword. Here's a comparison between what you currently have and code using the new keyword:

    yield_from_test.py:

    def gen_for(a_list):
        if a_list:
            yield a_list[0]
            for x in gen_for(a_list[1:]):
                yield x
    
    def gen_yield(a_list):
        if a_list:
            yield a_list[0]
            yield from gen_yield(a_list[1:])
    
    if __name__ == '__main__':
        assert list(gen_for([1,2,3])) == list(gen_yield([1,2,3]))
        print(list(gen_yield([1,2,3])))
    

    » python3 yield_from_test.py [1, 2, 3]

    0 讨论(0)
  • 2021-01-07 19:58

    Your code sample is very idiomatic and concise, no need and no real chance for further improvements and especially not when it comes at readability.

    0 讨论(0)
  • 2021-01-07 19:59

    Your first example is the proper solution.

    0 讨论(0)
  • 2021-01-07 20:03

    There's been some call for a yield from or the like that "passes through" all the values returned by a subgenerator. See PEP 380 for some ideas that have been bounced around. However, nothing has been implemented yet. Your first example is correct.

    0 讨论(0)
提交回复
热议问题