I have a generator and I would like to know if I can use it without having to worry about stopiteration and I For example, to use it without the generator for items
For example, I want to experiment with it a bit (or other construction) how to do it?
Use this to wrap your generator:
class Generator wipe (object): def __init __ (self, generator): self.generator = generator def __iter __ (self): return self DEF next (self): self in self.generator: increase o returns StopIteration # if you Do not care about Don Itter Protocol, remove this line and __iter__ method.
Use it like this:
def example_generator (): I [1,2,3,4,5] in: yield i gen = GeneratorWrap (example_generator ()) print gen.next () # Print 1 print gen.next () # Print2
Update: Please reply below Use it because it is better than one.
Comments
Post a Comment