We have contacted the parameters (arguments) of the function (functions) pass. At that time we passed the corresponding parameters according to the position. We will be in touch with more parameter transfer modes.
Recall the location pass:
Copy Code code as follows:
def f (a,b,c):
Return A+b+c
Print (f (1,2,3))
When the f is invoked, 1,2,3 is passed to the A,b,c, respectively, according to position.
Key word Delivery
In some cases, it is more rigid to use position transfer. A keyword (keyword) pass is a parameter passed according to the name of each parameter. The keyword does not use the corresponding relationship of the position. Still follow the definition of f above, change the invocation mode:
Copy Code code as follows:
Keyword passing can be mixed with position passing. But the positional arguments are preceded by the keyword parameter:
Copy Code code as follows:
Parameter Default value
When you define a function, you can assign a default value to the parameter using the form a=19. If the parameter is not eventually passed the value, the default value is used.
Copy Code code as follows:
def f (a,b,c=10):
Return A+b+c
Print (f (3,2))
Print (f (3,2,1))
In the first call to function f, we do not have enough values, C is not assigned, C will use the default value of 10.
The second time the function is invoked, C is assigned a value of 1, and the default value is no longer used.
Parcel Pass
When defining a function, we sometimes do not know how many arguments are passed when invoked. At this point, the parcel (packing) positional parameters, or the package keyword parameters, are useful for parameter passing.
Here is an example of parcel placement delivery:
Copy Code code as follows:
def func (*name):
Print type (name)
Print Name
Func (1,4,6)
Func (5,6,7,1,2,3)
Two calls, although the number of parameters are different, are based on the same Func definition. In the Func parameter table, all parameters are collected by name and merged into a tuple (tuple) based on the location, which is where the parcel is delivered.
To remind the Python parameter, name is the tuple name used for the parcel location pass, and the * before name when defining Func.
The following is an example of a package keyword transfer:
Copy Code code as follows:
def func (**dict):
Print type (dict)
Print Dict
Func (a=1,b=9)
Func (m=2,n=1,c=11)
Like the example above, Dict is a dictionary that collects all the keywords and passes them to the function func. To remind Python that the parameter dict is the dictionary used to wrap the keyword transfer, add * * before the dict.
The key to parcel delivery is to add * or * * to the corresponding tuple or dictionary before defining the function.
Solution Package
* and * *, can also be used at the time of the call, that is, the solution package (unpacking), the following example:
Copy Code code as follows:
def func (a,b,c):
Print A,b,c
args = (1,3,4)
Func (*args)
In this case, the so-called solution package is to have each element of tuple correspond to a positional parameter when passing tuple. Use * When calling Func to remind Python: I want to split the args into three discrete elements, passed to A,b,c. (Imagine what will happen to args before the Func is invoked?) )
Accordingly, there is also the solution package for the dictionary, using the same Func definition, and then:
Copy Code code as follows:
Dict = {' A ': 1, ' B ': 2, ' C ': 3}
Func (**dict)
When the dictionary dict is passed, each key value of the dictionary is passed to func as a keyword.
Mixed
When you define or invoke a parameter, several ways in which the parameters are passed can be mixed. But be careful in the process before and after the order. The basic principle is, first position, then the keyword, and then parcel position, and then parcel the key words, and according to the principle of the above said to carefully distinguish.
Note: Note the distinction between definition and invocation. Parcel and reconciliation package is not the opposite operation, is two relatively independent process.
Summarize
keyword, default value,
Parcel position, parcel key word
Solution Package