Is there a Python function that checks if a generator is started?

前端 未结 4 440
轻奢々
轻奢々 2021-01-05 00:46

I try to define a generator function mycount() that can be reset with the generator function send(0) as in the example below. Everything works fine

4条回答
  •  无人及你
    2021-01-05 01:30

    In particular, you might find a way to use the consumer decorator described on p. I-131 of David Beazley's "Generator Tricks," to which J. Gwyn provided a link:

    def consumer(func):
        def start(*args,**kwargs):
            c = func(*args,**kwargs)
            c.next()
            return c
        return start
    

    I use something similar in my code.

    Note that if v is None is preferred over if v == None.

提交回复
热议问题