How can I run the initialization code for a generator function immediately, rather than at the first call?

后端 未结 5 856
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 07:46

I have a generator function that goes something like this:

def mygenerator():
    next_value = compute_first_value() # Costly operation
    while next_value          


        
5条回答
  •  旧时难觅i
    2020-12-31 08:32

    For my use case I used a modified version of @ncoghlan answer but wrapped in a factory function to decorate the generating function:

    import collections, functools, itertools
    
    def primed_generator(generating_function):
        @functools.wraps(generating_function)
        def get_first_right_away_wrapper(*args,**kw):
            "call the generator function, prime before returning"
            gen = generating_function(*args,**kw)
            assert isinstance(gen,collections.Iterator)
            first_value = next(gen)
            return itertools.chain((first_value,),gen)
        return get_first_right_away_wrapper
    

    Then just decorate the function:

    @primed_generator
    def mygenerator():
        next_value = compute_first_value() # Costly operation
        while next_value != terminating_value:
            yield next_value
            next_value = compute_next_value()
    

    and the first value will be calculated immediately, and the result is transparent.

提交回复
热议问题