Skip to content

Instantly share code, notes, and snippets.

@ericzhong
Created May 23, 2017 12:41
Show Gist options
  • Save ericzhong/ebaf685acf2832c7d055c7b23c35a1ba to your computer and use it in GitHub Desktop.
Save ericzhong/ebaf685acf2832c7d055c7b23c35a1ba to your computer and use it in GitHub Desktop.
生成器 send() 方法
def gen(stop):
i = 0
while True:
if i >= stop:
raise StopIteration
i = (yield i) or i+1
g = gen(100)
print(next(g)) # Python 3 compatibility
print(next(g))
g.send(5)
print(next(g))
g.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment