Skip to content

Instantly share code, notes, and snippets.

@schnipdip
Last active September 19, 2020 16:19
Show Gist options
  • Save schnipdip/94dabb1acb224ca870fc867877dd0007 to your computer and use it in GitHub Desktop.
Save schnipdip/94dabb1acb224ca870fc867877dd0007 to your computer and use it in GitHub Desktop.
Generator Objects
'''
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