Describes the variable parameter definitions of Python functions and the parameter passing methods.
Variable Parameter definition of Python functions and parameter transmission methods
The definition of variable parameters of functions in python is as follows:
1. func (* args)
The input parameters exist in args as tuples, for example:
Def func (* args): print args >>>> func (1, 2, 3) (1, 2, 3) >>> func (* [1, 2, 3]) # In this method, all elements in a list can be passed as an indefinite parameter (1, 2, 3)
2. func (** kwargs)
The input parameters exist in args as dictionaries, for example:
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 }) # In this way, all key-value pairs in a dictionary can be passed as keyword parameters {'A': 1, 'C': 3, 'B': 2}
3. You can also mix the two func (* args, ** kwargs)
The input sequence must be the same as the defined sequence. Here, the parameter list is not fixed, and then the keyword parameter dictionary, for example:
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 (, 3, a = 4, B = 5, c = 6) (1, 2, 3) {'A': 4, 'C': 6, 'B ': 5} </span> # Skip transfer is not feasible> func (1, 2, 3, a = 4, B = 5, c = 6, 7) SyntaxError: non-keyword arg after keyword arg
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!