Created
May 29, 2020 02:17
-
-
Save ZhouYang1993/43bbe4abdf03e1f294e0df54bea7c1be to your computer and use it in GitHub Desktop.
Generators in Python
This file contains hidden or 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 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