生成器
Posted kuraki
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成器相关的知识,希望对你有一定的参考价值。
def fib(num): a, b = 0, 1 index = 0 while index < num: yield a a, b = b, a+b index += 1 return ‘ok...‘ f = fib(100) while True: try: print(next(f)) except Exception as e: print(e.value) break # for num in f: # print(num)
def fib(num): a, b = 0, 1 index = 0 while index < num: temp = yield a print(‘temp:‘,temp) a, b = b, a+b index += 1 f = fib(20) ret = next(f) print(ret) ret2 = f.send(‘test‘) print(ret2)
以上是关于生成器的主要内容,如果未能解决你的问题,请参考以下文章