生成器.next
转自:http://www.cnblogs.com/python-life/articles/4549996.html#3241763
send(msg) 与 next()的返回值
程序的详细解释如下(用pycharm里的debug,就能详细的看代码的运行过程):
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的值