Python: Splat/unpack operator * in python cannot be used in an expression?

十年热恋 提交于 2019-11-28 00:39:29
B. M.

Unpacking in list, dict, set, and tuple literals has been added in Python 3.5, as described in PEP 448:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) on Windows (64 bits).

>>> [1, 2, 3, *[4, 5, 6]]
[1, 2, 3, 4, 5, 6]

Here are some explanations for the rationale behind this change. Note that this does not make *[1, 2, 3] equivalent to 1, 2, 3 in all contexts. Python's syntax is not intended to work that way.

Andriy Ivaneyko

Asterix * isn't simply unary operator, it's argument-unpacking operator for functions definitions and functions calls.

So * supposed to be used only to work with function params and not with lists, tuples etc.

NOTE: starting from python3.5, * could be used not only with functions params, @B. M's answer greatly describes that change in python.

If you need to concat lists use concatenation instead list1 + list2 to get desired result. To concatenate list and generator simply pass generator to list type object, prior concatenating with another list:

gen = (x for x in range(10))
[] + list(gen)

This is not supported. Python 3 gives a better message (though Python 2 does not support * in the left part of an assignment, afaik):

Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
>>> [1,2,3, *[4,5,6]]
  File "<stdin>", line 1
SyntaxError: can use starred expression only as assignment target
>>> 

f(*[4,5,6]) is equivalent to f(4,5,6)

Function argument unfolding is a special case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!