python yield from
def kk(): print (1) yield print (2) return 3 k=kk() def hello(k): print("Hello world!") r = yield from k print(r,"Hello again!") yield he=hello(k) next(he) print ('---') next(he)
打印结果:
hello world!
1
---
2
3 hello again!
说明:1.yield from后面必须跟可迭代对象
2.yield from会把from后面的迭代器带入当前迭代器中进行迭代
3.,为了更好的理解yield from的作用,以下用一个等效于上述代码执行流程的代码加以解释,上述代码的执行过程等同于:
def hello(): print("Hello world!") print (1) yield print (2) r=3 print(r,"Hello again!") yield he=hello() next(he) print ('---') next(he)