Last active
May 29, 2020 02:12
-
-
Save ZhouYang1993/5cd538739ecd3aedb2c9cd6bf558abc0 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 my_generator(maximum): | |
| n = 0 | |
| while n < maximum: | |
| yield n | |
| return 'Done' | |
| print(type(my_generator)) | |
| # <class 'function'> | |
| print(type(my_generator(5))) | |
| # <class 'generator'> | |
| print(next(my_generator(5))) | |
| # 0 | |
| print(next(my_generator)) | |
| # 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> | |
| # TypeError: 'function' object is not an iterator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment