Skip to content

Instantly share code, notes, and snippets.

@lachlan-eagling
Created October 5, 2019 01:52
Show Gist options
  • Save lachlan-eagling/45432f5234366ddc39150bdbbc2bc7cf to your computer and use it in GitHub Desktop.
Save lachlan-eagling/45432f5234366ddc39150bdbbc2bc7cf to your computer and use it in GitHub Desktop.
Blog - Generators (StopIteration)
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