随笔关于yield,转载

原文链接,深入理解yield

def h():
    print ('Wen Chuan',)
    m = yield 5  # Fighting!
    print (m)
    d = yield 12
    print ('We are together!')

c = h()
m = next(c)  #m 获取了yield 5 的参数值 5
print(m)
d = c.send('Fighting!')  #d 获取了yield 12 的参数值12
print ('We will never forget the date', m, '.', d)

输出结果为:

Wen Chuan
5
Fighting!
We will never forget the date 5 . 12
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章