Created
October 5, 2019 01:52
-
-
Save lachlan-eagling/45432f5234366ddc39150bdbbc2bc7cf to your computer and use it in GitHub Desktop.
Blog - Generators (StopIteration)
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
generator = sequence_generator(3) | |
try: | |
print(next(generator)) | |
print(next(generator)) | |
print(next(generator)) | |
print(next(generator)) # This call to next will raise an exception. | |
except StopIteration: | |
print("Generator exhausted") | |
>>> 0 | |
>>> 1 | |
>>> 2 | |
>> Generator exhausted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment