This article gives an example of the python function variable parameter definition and its parameter passing method. Share to everyone for your reference. The specific analysis is as follows:
The function indeterminate parameters in Python are defined in the following form:
1, func (*args)
The arguments passed in are args in a tuple form, such as:
?
1 2 3 4 5 6 |
def func (*args): Print args >>> func (1,2,3) (1, 2, 3) >>> func (*[1,2,3)) #这个方式可以直接将一个列表的所有元素当作不定参数 incoming (1, 2, 3) |
2, func (**kwargs)
The arguments passed in are args in the form of a dictionary, such as:
?
1 2 3 4 5 6 |
def func (**kwargs): Print Kwargs >>> func (a = 1,b = 2, c = 3) {' A ': 1, ' C ': 3, ' B ': 2} >>> func (**{' a ': 1 , ' B ': 2, ' C ': 3} ' #这个方式可以直接将一个字典的所有键值对当作关键字参数传入 {' A ': 1, ' C ': 3, ' B ': 2} |
3, can also be mixed with the two
Func (*args, **kwargs)
The order of incoming must be the same as the order of definition, here is an indefinite argument list, and then a dictionary of keyword parameters, such as:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 |
def func (*args, **kwargs): Print args print Kwargs >>> func (1,2,3) (1, 2, 3) {} >>> func (*[1,2,3)) (1, 2 , 3) {} >>> func (a = 1, b = 2, c = 3) () {' A ': 1, ' C ': 3, ' B ': 2} >>> func (**{' a ': 1, ' B ': 2, ' C ': 3} ') () { ' A ': 1, ' C ': 3, ' B ': 2} >>> func (1,2,3, a = 4, b=5, c=6) (1, 2, 3) {' A ': 4, ' C ': 6, ' B ': 5}</span> #这样跳跃传递是 No. >>> func (1,2,3, a=4, b=5, c=6, 7) syntaxerror:non-keyword arg after keyword arg |
I hope this article will help you with your Python programming.