Skip to content

Instantly share code, notes, and snippets.

@pybites
Created December 15, 2021 18:37
Show Gist options
  • Save pybites/ed4b53367303527839e20d85c09ca6fc to your computer and use it in GitHub Desktop.
Save pybites/ed4b53367303527839e20d85c09ca6fc to your computer and use it in GitHub Desktop.
>>> 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