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

后端 未结 3 1855
逝去的感伤
逝去的感伤 2020-12-03 20:40

Does anybody know the reasoning as to why the unary (*) operator cannot be used in an expression involving iterators/lists/tuples?

Why is it only limite

3条回答
  •  既然无缘
    2020-12-03 21:32

    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 "", 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.

提交回复
热议问题