我们不能在 python 生成器函数中产生多个值吗?
例子,
In [677]: def gen():
.....: for i in range(5):
.....: yield i, i+1
.....:
In [680]: k1, k2 = gen()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-680-b21f6543a7e9> in <module>()
----> 1 k1, k2 = a()
ValueError: too many values to unpack
这工作如下:
In [678]: b = a()
In [679]: list(b)
Out[679]: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
即使我这样做,结果也一样:
In [692]: def a():
for i in range(5):
yield i
yield i+1
谢谢。