1. Definition of parameters
1、 Where are the parameters of the function defined
When defining a function in python, the parentheses after the function name are used to define the parameters. If there are multiple parameters, the parameters are directly separated by commas.
case:
# Using the parameters of the function, define a function that can add any two numbers
def add_num(a,b):
c = a + b
print(c)
**2、 Function call with parameters: **
The function defines the parameters, so you need to pass in the parameters when calling the function
add_num(11,22)
operation result
33
In the above case, the parameters defined in parentheses after the function name when we define the function are called formal parameters.
When we call a function, the parameters passed in are called actual parameters, and formal parameters are used to receive actual parameters.
Two, parameter classification
Above we talked about formal parameters and actual parameters. When defining and calling formal parameters and actual parameters, they can be divided into several categories according to different forms.
1、 Classification according to actual parameters
Actual parameters: positional parameters, keyword parameters
1.1、 Positional parameters (unnamed parameters)
def func(a,b,c):print(a)print(b)print(c)add_num(11,22,33)
# operation result
112233
In the above case, the three formal parameters of the function are to receive the actual parameters passed in by position. We call this form of passing parameters as positional parameters.
1.2、 Keyword parameters (named parameters)
def func(a,b,c):print(a)print(b)print(c)add_num(11,c=99,b=33)
# operation result
113399
When calling a function, the actual parameter is specified by the parameter name and passed to a certain formal parameter. This form of passing parameters, we call it a keyword parameter
Note: When passing parameters, write positional parameters first, and then write named parameters
2、 Classification according to formal parameters
Formal parameters can be divided into three categories: mandatory parameters, silent parameters, and variable length parameters
2.1、 Required parameters:
Parameters that must be passed when calling the function
def add(a,b):
c=a+b
print(c)add(11,22)
The a and b in the above function are necessary parameters, which must be passed when calling the function, otherwise an error will be reported
2.2、 Default parameters (default parameters):
When calling a function, you can pass it or not, or use the default value if you don’t pass it
def func(a,b,c=99):print(a)print(b)print(c)
func(11,22,33)print('-----------')func(55,66)
# operation result:
112233---------------556699
From the above cases, we can find that when we call for the first time, we pass in three parameters, c prints out the value we passed in, the second time only passes in two parameters, this time c prints What comes out is the default value we set when we defined
Note: The parameters with default values must be at the end of the parameter list.
2.3、 Variable length parameters *args and **kwargs
You can pass 0 or more when calling a function
2.3.1、* args: Receive multiple incoming positional parameters and save them in the form of an ancestor
def func(*args): print(args)func(33,44,55,66,77)func(*(33,44,55,66,77))#operation result(33,44,55,66,77)(33,44,55,66,77)
You can pass in directly when calling: func(33,44,55,66,77),
You can also assemble the list or tuple first, and then pass in through unpacking: func((33,44,55,66,77));
2.3.2、**kwargs: Receive multiple incoming keyword parameters and save them in the form of a dictionary
def func(**kwargs):print(kwargs)func(e=33,h=44,f=55,d=66,c=77)func(**{'e':33,'h':44,'d':66,'c':77})
# operation result
{' e':33,'h':44,'f':55,'d':66,'c':77}{'e':33,'h':44,'f':55,'d':66,'c':77}
**kwargs, kw receives a dictionary;
Keyword parameters can be directly passed in: func(11,22,e=33,h=44,f=55,d=66,c=77),
You can also assemble the dict first, and then pass it through unpacking: func({'e':33,'h':44,'d':66,'c':77}).
Note: Using *args and **kwargs is Python's idiom. Of course, other parameter names can also be used, but idioms are better.
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts