Python yield用法原理的基本测试

Demo1:

def cs(p):
    a=1
    print('cs:',p)
    while True:
        a = yield a+100
        print(p,a)        
#
g=cs('htzd')
res=g.send(None)  # next(g)
print(res)
#
res=g.send(1000)
print(res)
#
res=g.send(2000)
print(res)

Demo2:

def dog(name):
    food_list=[]
    print('道哥%s准备吃东西啦...' %name)
    while True:
        # x拿到的是yield接收到的值
        x = yield food_list # x = '肉包子'
        print('道哥%s吃了 %s' %(name,x))
        food_list.append(x) # ['一根骨头','肉包子']
#
g=dog('alex')
res=g.send(None)  # next(g)
print(res)
#
res=g.send('一根骨头')
print(res)
#
res=g.send('肉包子')
print(res)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值