Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
>>> def gen():
... yield from [1, 2, 3]
...
>>> g = gen()
>>> for i in g: print(i)
...
1
2
3
# generator exhausted:
>>> for i in g: print(i)
...
# reusable generator:
>>> class Gen:
... def __iter__(self):
... yield from [1, 2, 3]
...
>>> g = Gen()
>>> for i in g: print(i)
...
1
2
3
# now you can iterate again:
>>> for i in g: print(i)
...
1
2
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment