Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
We have been involved in functionsParameter (arguments) TransferAt that time, we mentioned that the parameter is based onLocationCorrespondingPassed. This time, we are ready to use more parameter matching methods.
First, let's recall that it is passed by location:
DefF (a, B, c ):PrintA, B, CF (1, 2, 3)
When F is called, 1, 2, 3 are passed to A, B, and C respectively based on their locations.
1. Key Word parameter transfer
Sometimes, we find thatProgramWhen using location transfer, it will feel rigid.Keyword transfer is to pass the value based on the name of each parameterWithout having to follow a fixed position. The above f definition is still used to change the call method:
F (C = 3, B = 2, A = 1)
Keyword transfer can be mixed with location transfer, but the location transfer parameter must be prior to the parameter passed by the keyword.
F (1, C = 3, B = 2)
2. Default Value)
InDefine functionsIf a = 19 is used, you can assign the default value to the parameter. If this parameter is not passed, the default value is used.
DefF (a, B, c = 10):PrintA, B, CF (3, 2)
F (3, 2, 1)
When function f is called for the first time, we do not have enough values. C is not assigned a value, and C uses the default value 10.
When the second function is called, C is assigned a value of 1 and the default value is no longer used.
3. Packing location transfer and packing keyword transfer:
When defining a function, we sometimes do not know how many functions will be passed during the call. In this case, it is very useful to use parcel location transfer and parcel keyword transfer.
Below isParcel location TransferExample:
DefFunc (*Name ):PrintType (name)PrintNamefunc (1, 4, 6)
Func)
The preceding two calls can be defined using the same func even though the number of parameters is different. The reason is that,In the func parameter table, all parameters are collected by names and merged into a value table (tuple) based on their locations)This is the parcel location transfer. To remind the python parameter name to be the value table name used for parcel location transfer, when defining func,Add the * sign before the name.
Below isTransfer of package keywordsExample:
DefFunc (**Dict ):PrintType (dict)PrintDictfunc (= 1, B = 9) Func (M= 2, n = 1, C = 11)
Similar to the preceding example, dict is a dictionary that collects all the keywords and passes them to the function func. To remind the python parameter dict,Add ** before dict **.
We can see that the key to parcel location transfer and parcel keyword transfer isWhen defining a functionAdd ** or ** to the corresponding value table or dictionary **.
4. Unpack the package
* And ** inWhen callingApplication, mainlyUnpacking ),The following is an example:
DefFunc (A, B, C ):PrintA, B, c
ARGs = (1, 3, 4)
Func (* ARGs)
In this example, the so-called solution package,When a tuple is passed, each element of the tuple corresponds to a location parameter.. Here, we use * when calling func to remind Python that I want to split ARGs into three scattered elements and pass them to A, B, and C respectively. (Imagine that when calling func, what are the consequences of not * Before ARGs ?)
Correspondingly, there is a solution package for the dictionary, using the same func definition, and then:
Dict = {'A': 1,'B': 2,'C': 3} Func (** Dict)
When passing the dictionary dict, each key-value pair of the dictionary is passed as a keyword to func..
5. Mixed definition and mixed call
You can define and call parameters in a hybrid manner. However, be careful about the order in the process. The basic principle is,First place, then keyword, then package location, then wrap keywordAccording to the principles described above.
Note: note the differences between the definition and the call. Package settlement and package operations are not the opposite, but two relatively independent processes.
Summary:
Keyword, default value,
Package location, package keyword
Unpack