Last active
September 19, 2020 16:19
-
-
Save schnipdip/94dabb1acb224ca870fc867877dd0007 to your computer and use it in GitHub Desktop.
Generator Objects
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 functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. | |
Objects are not stored in memory. | |
''' | |
def generator_function(): | |
counter = 0 | |
x = 10 | |
while counter < x: | |
n = x += 1 | |
yield n | |
n = generator_function() | |
print(next(n)) | |
''' | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment