Skip to content

Instantly share code, notes, and snippets.

@kshirsagarsiddharth
Created July 27, 2020 07:26
Show Gist options
  • Save kshirsagarsiddharth/1f53f9c6d14807c6085a915fc1f6557c to your computer and use it in GitHub Desktop.
Save kshirsagarsiddharth/1f53f9c6d14807c6085a915fc1f6557c to your computer and use it in GitHub Desktop.
example of a generator function
def generator_function():
value = 1
print("This is printed first")
yield value
value += 1
print("This is printed second")
yield value
value += 1
print("This is printed third")
yield value
iter_object = generator_function()
print(next(iter_object))
print(next(iter_object))
print(next(iter_object))
print(next(iter_object))
"""
This is printed first
1
This is printed second
2
This is printed third
3
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
in
3 print(next(iter_object))
4 print(next(iter_object))
----> 5 print(next(iter_object))
StopIteration:
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment