python yield的用法

it2022-05-09  21

next方法

#coding=utf-8 from time import sleep def test(l1): for l in l1: print l sleep(5) yield 'result:%s' % l l1=[1,2,3,4,5] t=test(l1=l1) for l in l1: print t.next()

  

send用法,注意get为参数和return的结果,无需单独写return

#coding=utf-8 from time import sleep def test(): get='' while True: l = yield get if l: print l sleep(2) get= 'result:%s' % l else: return l1=[1,2,3,4,5] t=test() t.send(None) for l in l1: r= t.send(str(l)) print r

  

转载于:https://www.cnblogs.com/edwar172038/p/10395009.html

相关资源:数据结构—成绩单生成器

最新回复(0)