为什么生成器中arg的值始终为None?

时间:2017-09-08 02:32:17

标签: python yield

>>> def func(n):
...     for i in range(0,n):
...         arg= yield i
...         print(i,arg)
... 
>>> f=func(3)
>>> while True:
...     print('main',next(f))
... 
main 0
0 None
main 1
1 None
main 2
2 None

为什么结果不是以下?

main 0
0 0
main 1
1 1
main 2
2 2

表示arg = yield i,如果i = 0,则arg = 0,print(i,arg)将输出0,0
对于arg = yield i,如果i = 1,则arg = 1,print(i,arg)将输出1,1
对于arg = yield i,如果i = 2,则arg = 2,print(i,arg)将输出2,2

0 个答案:

没有答案