The problem of variable parameters and position parameters of Python 3

def f1(a, b, c=0, *args, **kw):
    print("a =", a, "b =", b, "c =", c, "args =", args, "kw =", kw)
    
    
args = (1, 2, 3, 4)
kw = {"d": 99, "x": "-sharp"}
f1(*args, **kw)

the result is a = 1 b = 2 c = 3 args = (4,) kw = {"dink: 99," x sharp":"- sharp"}

here I think the reason for reporting an error is as follows:
a, b, c is a position parameter, args is a variable parameter, and * kw is a keyword parameter. Code

f1(*args, **kw)

only passes variable parameters and keyword parameters, but does not pass the three position parameters of a _ c. So as far as I understand it, the program should report an error, but this is not the case. And ask the god for advice

Mar.03,2021

so-called variable parameters and positional parameters are the same thing. In fact, there are only two types of parameters in python. The * of the * args parameter you pass to the F1 function means to expand the things in args as bit parameters in order, while the parameter table of F1 is (a, b, c, * args, * * kw) , a, b, c "captured" to 1,2,3 , * args . Of course, these are only artificial rules. You can say that the parameter expanded using * is the third type of parameter, but then you have to use another expression to replace the original asterisk expression to expand the position parameter, and you have to change the python source code

yourself.
Menu