Created
May 23, 2017 12:41
-
-
Save ericzhong/ebaf685acf2832c7d055c7b23c35a1ba to your computer and use it in GitHub Desktop.
生成器 send() 方法
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 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