标题想了很久,最终还是直接表述了。O (∩_∩)O

Python 3 的可迭代解包

PEP 3132 - Extended Iterable Unpacking 里面描述了一种对可迭代对象的解包用法,Python 3 可用:

In : a, *b, c = range(5)

In : a, c
Out: (0, 4)

In : b
Out: [1, 2, 3]

In : *a, = range(5)

In : a
Out: [0, 1, 2, 3, 4]

In : for a, *b in [(1, 2, 3), (4, 5, 6, 7)]:
...:     print(b)
...:
[2, 3]
[5, 6, 7]

挺方便的,ES6 也有对应的destructuring assignment语法实现解包数组,不过人家实现的更彻底,还可以解包对象。

不过在 Python 3.2 时引入了一个 BUG。看一个例子:

In : def a():
...:     rest = (4, 5, 6)
...:     t = 1, 2, 3, *rest
...:     return t
...:

In : for i in a():
...:     print(i)
...:
1
2
3
4
5
6

其实这是一个非常简化的写法,要不然需要把 1,2,3 放在一个元组中,再 + rest:

In : (1, 2, 3) + (3, 4, 5, 6)
Out: (1, 2, 3, 4, 5, 6)

但是上面这个例子稍微改一下:

In : def b():
...:     rest = (4, 5, 6)
...:     return 1, 2, 3, *rest
File "<ipython-input-38-b5a7115853e2>", line 3
  return 1, 2, 3, *rest
                    ^
SyntaxError: invalid syntax

不用变量 t, 直接返回就会抛 SyntaxError,另外就是 yield (当然要注意 return 和 yield 本身的意见不同啊):

In : def c():
...:     rest = (4, 5, 6)
...:     yield 1, 2, 3, *rest
File "<ipython-input-39-cf6991fcff64>", line 3
  yield 1, 2, 3, *rest
                   ^
SyntaxError: invalid syntax

Python 3.8

在 Python 3.8,修复了这个问题:

>>> def b():
...     rest = (4, 5, 6)
...     return 1, 2, 3, *rest
...
>>> for i in b():
...     print(i)
...
1
2
3
4
5
6
>>> def c():
...     rest = (4, 5, 6)
...     yield 1, 2, 3, *rest
...
>>> for i in c():
...     print(i)
...
(1, 2, 3, 4, 5, 6)

你学到了么?

延伸阅读

  1. PEP 3132
  2. Tuple unpacking in return and yield statements