Examples of Variable Parameter definitions and passing methods of Python functions, and details of python
This document describes how to define variable parameters of Python functions and how to transmit parameters. Share it with you for your reference. The specific analysis is as follows:
In python, variable function parameters are defined 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
I hope this article will help you with Python programming.