Unpacking arguments: only named arguments may follow *expression

后端 未结 6 525
长情又很酷
长情又很酷 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 10:01

    f is expecting 3 arguments (x, y, z, in that order).

    Suppose L = [1,2]. When you call f(3, *L), what python does behind the scenes, is to call f(3, 1, 2), without really knowing the length of L.

    So what happens if L was instead [1,2,3]?

    Then, when you call f(3, *L), you'll end up calling f(3,1,2,3), which will be an error because f is expecting exactly 3 arguments and you gave it 4.

    Now, suppose L=[1,2]1. Look at what happens when you callf`:

    >>> f(3,*L) # works fine
    >>> f(*L) # will give you an error when f(1,2) is called; insufficient arguments
    

    Now, you implicitly know when you call f(*L, 3) that 3 will be assigned to z, but python doesn't know that. It only knows that the last j many elements of the input to f will be defined by the contents of L. But since it doesn't know the value of len(L), it can't make assumptions about whether f(*L,3) would have the correct number of arguments.

    This however, is not the case with f(3,*L). In this case, python knows that all the arguments EXCEPT the first one will be defined by the contents of L.

    But if you have named arguments f(x=1, y=2, z=3), then the arguments being assigned to by name will be bound first. Only then are the positional arguments bound. So you do f(*L, z=3). In that case, z is bound to 3 first, and then, the other values get bound.

    Now interestingly, if you did f(*L, y=3), that would give you an error for trying to assign to y twice (once with the keyword, once again with the positional)

    Hope this helps

提交回复
热议问题