This article mainly introduces multiple methods for transferring function parameters in the python advanced tutorial, including keyword transfer, default value transfer, package location transfer, and mixed transfer of package keywords, if you need it, you can refer to the parameters (arguments) passed by function. At that time, we passed the corresponding parameters based on the location. We will be exposed to more parameter passing methods.
Recall location transfer:
The code is as follows:
Def f (a, B, c ):
Return a + B + c
Print (f (1, 2, 3 ))
When f is called, 1, 2, 3 are passed to a, B, and c respectively based on their locations.
Keyword transfer
In some cases, it may feel rigid to use location transfer. Key word transmission is based on the name of each parameter. Keyword does not follow the corresponding relationship of location. The above f definition is still used to change the call method:
The code is as follows:
Print (f (c = 3, B = 2, a = 1 ))
Keyword transfer can be mixed with location transfer. However, the location parameter must appear before the keyword parameter:
The code is as follows:
Print (f (1, c = 3, B = 2 ))
Default value
When defining a function, you can assign a default value to the parameter in the form of a = 19 ). If this parameter is not passed, the default value is used.
The code is as follows:
Def f (a, B, c = 10 ):
Return a + B + c
Print (f (3, 2 ))
Print (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.
Package transfer
When defining a function, we sometimes do not know how many parameters will be passed during the call. In this case, the packing location parameter or the packing keyword parameter is very useful for parameter transfer.
The following is an example of parcel location transfer:
The code is as follows:
Def func (* name ):
Print type (name)
Print name
Func (1, 4, 6)
Func)
Two calls, although the number of parameters is different, are defined based on the same func. In the func parameter table, all parameters are collected by name and merged into a tuple according to the position. this is the transfer of the package location.
To remind the Python parameter, name is the name of the tuples used for package location transfer. when defining func, add the * sign before name.
The following is an example of transfer of the wrap keyword:
The code is as follows:
Def func (** dict ):
Print type (dict)
Print dict
Func (a = 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 Python, the dict parameter is the dictionary used for the transfer of the wrap keyword, and ** is added before dict **.
The key to package transfer is to add * or ** before the corresponding tuples or dictionaries when defining functions **.
Unpack
* And ** can also be used when calling, that is, unpacking. The following is an example:
The code is as follows:
Def func (a, B, c ):
Print a, B, c
Args = (1, 3, 4)
Func (* args)
In this example, the so-called solution package enables each element of the tuple to correspond to a location parameter when transferring the tuple. * Is used when calling func to remind Python: 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:
The code is as follows:
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.
Hybrid
When defining or calling a parameter, you can mix several transmission methods of the parameter. However, be careful about the order in the process. The basic principle is to first place, then keyword, then package location, and then wrap the keyword, and to distinguish it according to the principle described above.
Note: pay attention to the distinction between definition time and call time. Package settlement and package operations are not the opposite. They are two relatively independent processes.
Summary
Keyword, default value,
Package location, package keyword
Unpack