轉自:http://www.cnblogs.com/python-life/articles/4549996.html#3241763html
send(msg) 與 next()的返回值
程序的詳細解釋以下(用pycharm裏的debug,就能詳細的看代碼的運行過程):python
def h():
print 'Wen Chuan',
m = yield 5 # Fighting!
print m
d = yield 12
print 'We are together!'
c = h()
m = c.next() # m 獲取了yield 5 的參數值 5
d = c.send('Fighting!') # d 獲取了yield 12 的參數值12
print 'We will never forget the date', m, '.', d第一行:實例化生成器c第二行:執行函數代碼直到yield 5處,並把yield 5的值賦給m第三行:發送'Fighting!'到yield表達式yield 5,此時m的值爲'Fighting!',而後打印m的值(print m),而後遇到下一個yield表達式yield 12,返回yield 12的值給d第四行:打印m,d的值