Variable length parameter list in python

Variable-length parameters can be implemented in python through keyword parameters and non-keyword parameters. Use ** to specify dictionary as keyword parameters, and * to specify tuples as non-keyword parameters.

1. Keyword parameters:

Define a function with non-keyword parameters:def func_name([位置参数或默认参数,] *non_keyword_args)

>>> def func1(arg1,arg2,*arg3):
		print('arg1:',arg1)
		print('aeg2:',arg2)
		for arg in arg3:
			print('非关键字参数:',arg)
>>> func1(1,2,3,'a',1,'b')
arg1: 1
aeg2: 2
非关键字参数: 3
非关键字参数: a
非关键字参数: 1
非关键字参数: b

Positional parameters (and default parameters) will be matched one by one in the order or the parameter values ​​will be given according to the parameter name. After matching the positional parameters and the default parameters, if there are additional parameters and the function declares non-keyword parameters, these The parameters are packed into tuples as non-keyword parameters.
In the above figure, arg1 matches 1, arg2 matches 2, and the remaining parameters are packed into tuples and passed to arg3.

2. Keyword parameters

Define a function with non-keyword parameters:def func_name([位置参数或默认参数,] **keyword_args)

>>> def func2(arg1,arg2,**arg3):
		print('arg1:',arg1)
		print('arg2:',arg2)
		for k in arg3.keys():
			print('关键字参数 [',k,' : ',arg3[k],']')
>>> func2(1,2,k1='v1',k2='v2',k3='v3')
arg1: 1
arg2: 2
关键字参数 [ k1  :  v1 ]
关键字参数 [ k2  :  v2 ]
关键字参数 [ k3  :  v3 ]

When the keyword parameters are defined in the function, after the positional parameters and the default parameters are matched, the remaining keyword parameters will be packaged into a dictionary and passed to the keyword parameters. arg11 matches 1, arg2 matches 2, and the remaining parameters are packed into a dictionary and passed to arg3.

In the above case, if the incoming parameters are matched with the positional parameters and the default parameters, there will be an error if there are parameters that cannot be matched by the function, such as:

>>> f1(1,2,3,'a'=1)
SyntaxError: keyword can't be an expression

>>> func2(1,2,3,k1='v1',k2='v2',k3='v3')
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    func2(1,2,3,k1='v1',k2='v2',k3='v3')
TypeError: func2() takes 2 positional arguments but 3 were given

3. Keyword parameters and non-keyword parameters

In the process of declaring the parameter list of a function, the order of various parameter declarations:

def func_name(位置参数, 默认参数, 非关键字参数, 关键字参数)

Note: The order cannot be changed!
Define the function func:

>>> def func(arg1, arg2, arg3='default', *arg4, **arg5):
	print('arg1:',arg1)
	print('arg2:',arg2)
	print('aeg3:',arg3)
	for arg in arg4:
		print('non-keyword arg:',arg)
	for k in arg5.keys():
		print('keyword arg [',k,':',arg5[k],']')

When passing parameters to the function, you can pack non-keyword parameters and keyword parameters in advance, for example:

>>> non_keyword_args = (1,2,3,4)
>>> keyword_args = {'k1':'v1','k2':'v2'}
>>> func(1,2,3,*non_keyword_args,**keyword_args)
arg1: 1
arg2: 2
aeg3: 3
non-keyword arg: 1
non-keyword arg: 2
non-keyword arg: 3
non-keyword arg: 4
keyword arg [ k1 : v1 ]
keyword arg [ k2 : v2 ]

However, what if this happens: func(1,2,3,4,5,k3='v3',*non_keyword_args,**keyword_args)
Run it and try it out:

>>> func(1,2,3,4,5,k3='v3',*non_keyword_args,**keyword_args)
arg1: 1
arg2: 2
aeg3: 3
non-keyword arg: 4
non-keyword arg: 5
non-keyword arg: 1
non-keyword arg: 2
non-keyword arg: 3
non-keyword arg: 4
keyword arg [ k1 : v1 ]
keyword arg [ k2 : v2 ]
keyword arg [ k3 : v3 ]

Note: The keyword and non-keyword parameters that are actually passed into the function are based on the parameters we actually pass in. Those other non-keyword and keyword parameters that are passed in will be packaged with the dictionary and tuple we passed in. As the final parameter passed in.

Guess you like

Origin blog.csdn.net/Miha_Singh/article/details/85009153