Created
December 15, 2021 18:37
-
-
Save pybites/ed4b53367303527839e20d85c09ca6fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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