Detailed explanation of parameters of functions in Python
1. Types of function parameters in Python
- Required parameters: parameters that must be passed in when calling the function, only the parameter name is defined when the function is defined
- Keyword parameters: When passed in, it is passed in as a parameter name value pair of the function
- Default parameter: When the function is defined, a value is set by default for the parameter. When the function is called, the parameter is not transmitted, that is, the default value is used.
- Variable length parameters: parameters modified with * or **; * modified parameters are a tuple (tuple), ** modified parameters must be a dictionary (dict), usually written as *args or **args
2, Python's required parameters
- The number of parameters passed in must be the same as the number of formal parameters
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
def demo01(a,b):
print(a, type(a))
print(b, type(b))
# demo01(1) #TypeError missing 1 required positional argument: 'b' is missing an unknown parameter, demo01 must pass 2 parameters
# demo01(1, 2, 3) #TypeError demo01() takes 2 positional arguments but 3 were given, demo01 function has 2 positional arguments but 3 were received;
# Required parameters: parameters that must be passed in when calling the function, only the parameter name is defined when the function is defined
# The number of parameters passed in must be the same as the number of formal parameters
demo01(1, 2) # yes
demo01(1, [1, 2]) # yes
demo01([2, 3], (1, 2)) # yes
demo01(1, {2, 3, 4}) # yes
demo01(2, {"code":'1001', "name":"zhang", "age":18}) # yes

3. Keyword Parameters
- The parameter is passed in the way of associating the parameter name and the parameter value, the mode of the key-value pair, and the parameter name is the key.
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
def demo01(a,b):
print(a, type(a), end="__")
print(b, type(b))
# The parameter before the parameter position of the first keyword entry and exit is selected by keyword parameter; in the following example, the variable passed by the first keyword is a, and a is in the second position of the function definition, then the first The parameters before the two positions must be passed in the form of keyword parameters.
# demo01(1, a=2) # demo01() got multiple values for argument 'a'
demo01(a=1, b=2) # yes
demo01(1, b=2) # yes
demo01(b=1, a=2) # yes
demo01(b=1, a=[1, 2]) # yes
demo01(b=[2, 3], a=(1, 2)) # yes
demo01(b=1, a={2, 3, 4}) # yes
demo01(b=2, a={"code":'1001', "name":"zhang", "age":18}) # yes

4. Default parameters
- When calling the function, the default value of the parameter is not passed in, an d it is processed according to the passed parameter value when it is passed in.
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
## default parameters
def demo02(City = "LongNan"):
print("City is the default parameter, the default value is: China,The current value is:", City)
# don't pass parameters
demo02()
# Incoming parameters
demo02("Beijing")

5. Variable length parameters
- The passed parameter will generate a tuple type variable for internal use of the function
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
## variable length parameter
def demo03(*args):
print(args,type(args))
# The passed parameter will generate a tuple type variable for internal use of the function
demo03(1)
demo03("code")
demo03(1,"code")

- The number of parameters passed in cannot be less than the number of required parameters
- The formal parameters after *args must be passed as keyword arguments
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
# The formal parameters after *args must be passed as keyword parameters,
def demo04(a, b, *args,c):
print("a The parameter values passed in are:{},b The parameter values passed in are:{}, args The parameters passed in are:{}, c The in and out parameters are:{}".format(a, b, args,c))
# The number of parameters passed in cannot be less than the number of mandatory parameters, a,b,c are mandatory parameters
# demo04(1, 2) # TypeError
# demo04(1, 2, 3) # TypeError
demo04(1, 2, c=3)

- Parameters before *args can only be passed by position, not by keyword
- When the number of incoming parameters is more than the required parameters, first assign the required parameters by position, and then generate a tuple for the remaining parameters and pass them to args

- **Modified parameters must be passed as keyword parameters, and the Python interpreter will generate a dictionary with the passed-in keyword and keyword value for internal use in the function
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
def demo05(**kwargs):
print("kwargs The parameters passed in are:{}".format(kwargs),type(kwargs))
kwargs = {"code":'1002', "name":"zhang"}
# demo05(kwargs) # TypeError
# **Modified parameters must be passed as keyword parameters, and the Python interpreter will generate a dictionary with the passed-in keyword and keyword value for internal use in the function
demo05(**kwargs) # kwargs The parameter passed in is: {'code': '1002', 'name': 'zhang'} <class 'dict'>
demo05(code='1002',name="zhang") # kwargs The parameter passed in is: {'code': '1002', 'name': 'zhang'} <class 'dict'>

- **The modified parameter must be the last one
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
# **The modified parameter must be the last one
"""
# SyntaxError: invalid syntax
def demo06(a, b, *args, c, **kwargs, d):
pass
"""
def demo06(a, b, *args, c, **kwargs):
print("a The parameter value passed in is:{},b The parameter value passed in is:{}, args The parameter passed in is:{}".format(a, b, args),end='')
print(",c The parameter passed in is:{},kwargs The parameter passed in is:{}".format(c,kwargs))
demo06(1, 2, 3, 4, 5, c=3, code='1002', name="zhang", d=3)
