In this paper, we describe the variable parameter definition of Python function and its parameter passing mode. Share to everyone for your reference. The specific analysis is as follows:
The parameters of a function in Python are defined in the following form:
1. Func (*args)
The passed in parameter is in the form of a tuple in args, such as:
def func (*args):
2. Func (**kwargs)
The arguments passed in are in the form of a dictionary in args, such as:
def func (**kwargs):
3, can also be mixed with
Func (*args, **kwargs)
The order of incoming must be the same as the order of the definition, here is the indefinite parameter list, and then the keyword parameter dictionary, such as:
def func (*args, **kwargs): print args print Kwargs >>> func (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 (a = 4, b=5, c=6) (1, 2, 3) {' A ': 4, ' C ': 6, ' B ': 5} #这样跳跃传递是不行的 & gt;>> func (a=4, b=5, c=6, 7) syntaxerror:non-keyword arg after keyword arg
Hopefully this article will help you with Python programming.