Unpacking arguments: only named arguments may follow *expression

后端 未结 6 526
长情又很酷
长情又很酷 2020-12-03 09:18

The following works beautifully in Python:

def f(x,y,z): return [x,y,z]

a=[1,2]

f(3,*a)

The elements of a get unpacked as if

6条回答
  •  Happy的楠姐
    2020-12-03 09:59

    It doesn't have to be that way. It was just rule that Guido found to be sensible.

    In Python 3, the rules for unpacking have been liberalized somewhat:

    >>> a, *b, c = range(10)
    >>> a
    0
    >>> b
    [1, 2, 3, 4, 5, 6, 7, 8]
    >>> c
    9
    

    Depending on whether Guido feels it would improve the language, that liberalization could also be extended to function arguments.

    See the discussion on extended iterable unpacking for some thoughts on why Python 3 changed the rules.

提交回复
热议问题