第十节 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)
标签:obj,create,生成器,send,current,num,第十节,print,ret 来源: https://www.cnblogs.com/kogmaw/p/12575511.html