第十节 next和send取生成器的值

# def create_num(all_num):
# a, b = 0, 1
# current_num = 0
# while current_num <= all_num:
# yield a
# a, b = b, a+b
# current_num += 1
# return 'ok....'
#
# obj = create_num(2)
#
# while True:
# try:
# ret = next(obj)
# print(ret)
# except Exception as a:
# print(a.value) #用来捕获有return返回值的时候
# break

def create_num(all_num):
a, b = 0, 1
current_num = 0
while current_num <= all_num:
a = yield a
print('yield a 的返回值:', a)
a, b = b, a+b
current_num += 1
return 'ok....'

obj = create_num(10)

ret = next(obj)
print(ret)

ret = obj.send(5)
print(ret)

ret = obj.send(5)
print(ret)
posted @ 2020-03-26 16:55  kog_maw  阅读(183)  评论(0编辑  收藏  举报