Python之协程函数

Python之协程函数

  什么是协程函数:如果一个函数内部yield的使用方法是表达式形式的话,如x=yield,那么该函数成为协程函数。

def eater(name):
    print('%s start to eat food' %name)
    food_list=[]
    while True:
        food=yield food_list
        print('%s get %s ,to start eat' %(name,food))
        food_list.append(food)

    print('done')


e=eater('钢蛋')
# print(e)

print(next(e))
print(e.send('奥尔良烤翅'))
print(e.send('香辣鸡腿堡'))
print(e.send('奥尔良堡'))

猜你喜欢

转载自www.cnblogs.com/george92/p/9100325.html