Unpacking arguments: only named arguments may follow *expression

后端 未结 6 540
长情又很酷
长情又很酷 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条回答
  •  青春惊慌失措
    2020-12-03 10:12

    Thanks to the PEP 448 - Additional Unpacking Generalizations,

    f(*a, 3)
    

    is now accepted syntax starting from Python 3.5. Likewise you can use the double-star ** for keyword argument unpacking anywhere and either one can be used multiple times.

提交回复
热议问题