Created
July 27, 2020 07:26
-
-
Save kshirsagarsiddharth/1f53f9c6d14807c6085a915fc1f6557c to your computer and use it in GitHub Desktop.
example of a generator function
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 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