19. The default variable and keyword parameters of the python function

Insert picture description here

Insert picture description here

There are several types of function parameters:

-Required parameters
That means if you don’t pass parameters, it will throw an error;
-The default parameter fun(a, b=3)
can be directly assigned a default value, then when calling this function at this time, you can Only one parameter is passed, the default parameter is not passed, but note that there can be no common mandatory parameters after the default parameter. If you have the default parameter in the middle, and you have another mandatory parameter, then your default parameter has little meaning , Because you have to pass a parameter, so it won't have it, so from the grammatical design it restricts you can not do this.
**-Variable parameter fun(fmt, *args) ** The
variable parameter can only be the last parameter, because it is a variable parameter. If it is in the middle, you don't know which one is to be passed to the variable parameter In fact, for Python, if you pass multiple values, it just puts them in a tuple and passes them in.

**- Keyword parameter print(fmt, *args, **kw) **

We can specify the interval symbol and the newline character. They are all passed through keyword parameters. The keyword parameter must be the last one, which means that the keyword parameter must also be after the variable parameter, and must be the last parameter. .
The setting of a variable parameter is to add an * asterisk to the variable parameter, and the keyword parameter to add two * asterisks.
After the keyword parameter is passed, it is actually a Dictionary, which is a dictionary with a corresponding key: value. The key: value is used to obtain such a value.

  • Required parameters and default parameters
    Insert picture description here

  • variable parameter:
    Insert picture description here

  • Keyword parameters
    If you pass the parameters through keywords, you cannot ensure that the user passes them. Of course, you can restrict him to pass them, but then it becomes a mandatory parameter, so you have to deal with it. Keyword parameters:
    Insert picture description here
    The reason for the error is that the two if statements are not in the scope of the function func4, so you need to talk about the indentation of the two if statements and align with the print in func4. Remember the print in the if statement Statements should be indented accordingly!
    Insert picture description here

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/109277303