首页 > 编程语言 > 详细

python yield的用法

时间:2019-02-18 13:32:29      阅读:253      评论:0      收藏:0      [点我收藏+]

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

  

python yield的用法

原文:https://www.cnblogs.com/edwar172038/p/10395009.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!