python之yield 的用法

转载:https://blog.csdn.net/mieleizhi0522/article/details/82142856

def foo():
    print("starting...")
    while True:
        res = yield 4    #yield 看成一个rutrue 
        print("res:",res)
g = foo()
print(next(g))
print("*"*20)
#print(next(g))
print(g.send(9)) #g.send可以直接转入参数进去
print(next(g))

  

posted @ 2020-05-20 14:34  Mr.zou  阅读(174)  评论(0编辑  收藏  举报