Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created May 29, 2020 02:17
Show Gist options
  • Save ZhouYang1993/43bbe4abdf03e1f294e0df54bea7c1be to your computer and use it in GitHub Desktop.
Save ZhouYang1993/43bbe4abdf03e1f294e0df54bea7c1be to your computer and use it in GitHub Desktop.
Generators in Python
def example():
print('step 1')
yield 1
print('step 2')
yield 2
print('step 3')
yield 3
g = example()
next(g)
# step 1
# 1
next(g)
# step 2
# 2
next(g)
# step 3
# 3
next(g)
# Traceback (most recent call last):
# File "/usr/lib/python3.6/code.py", line 91, in runcode
# exec(code, self.locals)
# File "<input>", line 1, in <module>
# StopIteration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment