This article mainly introduces the detailed description of the python function variable parameter definition and the parameters of the data transfer method, here provides an example code to help you learn to understand this part of the content, the need for friends can refer to the next
Python function variable parameter definition and its parameter passing way
The defined form of a function variable in Python is as follows
1. Func (*args)
The passed in parameter is in the form of a tuple in args, such as:
def func (*args): print args >>> func (1, 2, 3) >>> func (*[1,2,3]) # This method can directly pass all the elements of a list as an indeterminate parameter (1, 2, 3)
2. Func (**kwargs)
The arguments passed in are in the form of a dictionary in args, such as:
def func (**kwargs): print Kwargs >>> func (a = 1,b = 2, c = 3) {' A ': 1, ' C ': 3, ' B ': 2} >>> F UNC (**{' a ': 1, ' B ': 2, ' C ': 3}) #这个方式可以直接将一个字典的所有键值对当作关键字参数传入 {' A ': 1, ' C ': 3, ' B ': 2}
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}</span> #这样跳跃传递是不行的 >>> func (a=4, b=5, c=6, 7) Syntaxerror:non-keyword Arg after KEYW Ord Arg