The meaning of / and * are allowed in function parameters after python3.8

After python3.8, / and * are allowed in function parameters. / is used to indicate that some function parameters must use positional parameters instead of keyword parameters. The first meaning of * appears in function parameters can be expressed as Variable parameters are generally written as *args; for the * parameters that appear alone in the parameters, it means that the parameters after the * must be in the form of keyword parameters. Next, we will explain the usage in detail:

1/Usage of parameters

If you want the caller of the function to use only positional parameters at a certain parameter position and not use keyword parameters to pass parameters, then you only need to put a / after the desired position.

def f1(a, b, /):
    return a + b

For the above function, when calling f1, the parameters a and b can only be specific values, and cannot be passed by keywords, that is, f1(2, 3) is executed correctly and f1(a=2, 3) and f1( 2, b=3) An error will be executed.

2 *Usage of parameters

The first usage of * is more common, so I won't go into details here, and I will mainly explain the second usage of * in detail here. If you want to force the caller to use certain parameters, and you must pass them in the form of keyword parameters, then you only need to place an * in the position before the desired position.

def f1(a, *, b, c):
    return a + b + c

For the above function, the parameter a can be any value when calling, but the b and c parameters must be passed as keyword parameters, such as f1(1, b=4, c=5), otherwise an error will be reported.

def f2(a, *, b, c=5):
    return a + b + c

If the function is called in this case, the parameter a can have any value, but the parameter b must be passed as a keyword parameter, such as f2(2, b=3), but if you want to pass in the c parameter, then the c parameter The requirements of is the same as the b parameter in the form of keyword parameters, such as f2(2,b=3,c=4).

3 / and * both appear in the function parameters

For example, we define a function f

def f(a, b, /, c, *, d, e):
    print(a, b, c, d, e)

When we call the function f, the parameters a and b can be the values ​​supported by any type of python, that is, they cannot be passed in the form of keywords; c can be the values ​​supported by any type of python, and d, e can only be parameterized by keywords Pass the reference. The following calling methods will all throw exceptions

f(a=1, 2, c=3, d=4, e=5) # a不能以关键字参数传参

f(1, 2, 3, 4, e=5) # d只能以关键字参数传参

4 summary

If you want the caller to use the function not to use keyword parameters to pass parameters, then you only need to put these parameters in front of /; if you want the caller to use the function, they must use certain parameters, and they must be keywords Parameters are passed, then you only need to put these parameters after *.

Author: Yang Hangfeng
link: https: //www.zhihu.com/question/287097169/answer/453193254
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.
 

Guess you like

Origin blog.csdn.net/zilan23/article/details/109963021