There are many ways to pass parameters of functions, such as position parameters, keyword parameters, default parameters, and uncertain length parameters. However, the most common one we use is probably positional parameters. Let's understand our parameters
1. Position parameters
When calling a function, the compiler will pass the actual parameters of the function to the formal parameters in order of position, that is, pass the first actual parameter to the first formal parameter, pass the second actual parameter to the second formal parameter, and so on.
In fact, the passed parameters and defined parameters are the same order and number.
Give an example
def xuesheng(name,age,gender): print(f'The student's name is{name},Age is{age},Gender is{gender}') #This is the function we define, so when we call it, we should follow the order of name, age, and gender xuesheng('Li Hua',18,'male')
The output result is
The student's name is Li Hua, age is 18, gender is male
2. Keyword parameters
When passing values with position parameters, if there are multiple parameters in the function, it is not easy to remember the position and meaning of each parameter. You can use keyword parameters to pass them. Keyword parameter passing associates the actual parameter with the formal parameter through the format of "formal parameter = actual parameter" ("key = value"), and passes the parameter according to the name of the formal parameter.
Example
def xuesheng(name,age,gender): print(f'The student's name is{name},Age is{age},Gender is{gender}') xuesheng(name = 'Li Hua',age = 18,gender = 'male')
At this time, the order of age = 18 in brackets when calling is called is 'Li Hua',gender = 'Male' can be disrupted, and the output results will not change
The student's name is Li Hua, age is 18, gender is male
When we call the function, if there are positional parameters, the positional parameters must be in front of the keyword parameters, but the order between the keyword parameters can be changed. Remember that the positional parameters correspond one by one, don't write the following before the previous one.
def xuesheng(name,age,gender): print(f'The student's name is{name},Age is{age},Gender is{gender}') #Every time, the previous position parameters may only be name, not age and gender, the position parameters are in sequencexuesheng('Li Hua',gender = 'male',age = '18')
Keyword parameter function: It can make the function clearer and easier to use, and also clear the order requirements of parameters.
3. Default parameters (also known as default parameters)
When defining a function, you can specify the default value of the formal parameters. When calling the function, if the value is not passed to the formal parameters with the default value, the default value of the parameter is directly used; if the value is passed to the formal parameters with the default value, the actual parameter value will override the default value. But be aware that all positional parameters must appear before the default parameters, including function definitions and calls.
Example
def xuesheng(name,age,gender = 'male'): print(f'The student's name is{name},Age is{age},Gender is{gender}') xuesheng('Li Hua','18')
The result is the same as before. Li Hua's output information has not changed. If we change it to a female at the default value, the result will be a female (of course, you can also pass the value in when the default value is a male and change it to a female)
4. Indefinitely long parameters
If the number of parameters to be passed into the function is uncertain, you can use an indefinite length parameter. Indefinite length parameters are also called variable parameters, and the number of parameters received by this parameter can be arbitrarily changed. Its types are divided into positional delivery and keyword delivery.
Let me first explain in advance *args and **kwargs are not special parameters, but just variable names agreed by programmers. args is the abbreviation of arguments, indicating positional parameters; kwargs is the abbreviation of keyword arguments, indicating keyword parameters. In fact, it is not necessary to write *args and **kwargs. Only the previous * (asterisk) is necessary.
1. Position delivery
def test(*args): print(args) print(type(args)) #Call the function and pass any parametertest(1,2,3,'I','yes','Li Hua')
The indefinite length parameter *args is used to receive an uncertain number of positional parameters. All parameters passed in when calling the function are received by *args and saved as tuples.
#Exist in tuple form(1, 2, 3, 'I', 'yes', 'Li Hua') <class 'tuple'>
2. Keyword parameters
The passed parameters must be accepted in the form of "key = value".
def test(**kwargs): print(kwargs) print(type(kwargs)) #Call the function and pass any parametertest(a = 'No',b = 'like',c = 'composition')
The indefinite length parameter **kwargs is used to receive an uncertain number of keyword parameters. All parameters passed in when calling the function are received by **kwargs and saved in dictionary form.
{'a': 'No', 'b': 'like', 'c': 'composition'} <class 'dict'>
summary:
1 The position parameters must be in order
2 The keyword parameters must be in the form of "key = value". If used in conjunction with the position parameters, the position parameters must be placed before
3. The default parameters must be placed last.
4 The indefinite length parameters should be distinguished from the indefinite length of the keyword. The former is received in the form of a tuple, and the "*" number should not be forgotten.
The latter is received in dictionary form, and "**" cannot be forgotten
This is the end of this article about various ways to pass functions in Python. For more related contents for function parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!