python生成器

def a():
for i in range(10):
yield i


# # print(a().__next__())
# for i in a():
# print(i)


def b():
for i in range(10):
# print(i)
c = yield i
print(i)
# print(c)

ff = b()


def c():
a = yield
print(a)
yield

# for i in ff:
# # print(i+2)
# ff.send(i)
# print(i)


cc = c()
for i in cc:
recv = cc.send(5)
# print(recv)

yield一次输入多次输出
yield+send可以做输入也可以做输出
当执行c = yield时 需要next()或者send(none),当第二次执行时才会进行c=yield的赋值操作,当执行完第二次yield后会继续寻找下一个yield 所以需要用yield结束 让他等待
posted @ 2018-04-29 23:47  盈波秋水泛清涛  阅读(125)  评论(0编辑  收藏  举报