Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Last active May 29, 2020 02:12
Show Gist options
  • Save ZhouYang1993/5cd538739ecd3aedb2c9cd6bf558abc0 to your computer and use it in GitHub Desktop.
Save ZhouYang1993/5cd538739ecd3aedb2c9cd6bf558abc0 to your computer and use it in GitHub Desktop.
Generators in Python
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