Location Pass
We've made arguments to the function when we define the function, but it's just a superficial transfer of position .
Example
def sum (a,b,c): d = a+b+c return dprint(sum ()
When the function sum is called, the A,b,c is passed to the
Keyword delivery
Keyword passing is a parameter passed according to the name of each parameter
Print (SUM (c=3,b=2,a=1))
Keywords can be used in combination with location passing, but the positional parameters are before the keyword argument
Print (SUM (1,c=3,b=2))
Parameter Default value
When defining a parameter, the default value can be assigned to the parameter in the form of a a=10, which will be used if the parameter is not eventually passed.
def sum (b,c,a=10): d = a+b+c return dprint(sum (c=3,b=2))
Default values need to be written at the end, otherwise error!
The default value is used when the call does not have a value of a
The Print screen results are:
Parcel delivery
When defining a function, we do not know how many arguments to pass when the function is called. This is a very useful time to use parcel delivery.
Here is an example of parcel location passing
def func (*num): print(type (num)) print(num) func (1,2,3,44,56)
Two calls, although the number of arguments is different, is based on the function func definition. in the Func parameter table, all parameters are collected by name, and a tuple is synthesized based on the location, which is the parcel delivery
Add * before the tuple
Here is an example of parcel keyword delivery
def func (**dict): print(Type (dict)) print(dict) func (a=1,b=2,c=3) func (a=1,b=2 , c=3,d=44,e=56)
Dict is a dictionary that collects all the keywords, passing functions to func. To remind Python that the parameter dict is the dictionary used to pass the Parcel keyword,
Add * * Before Dict
Solution Package
* and * * can also be used at the time of the call
Here is an example:
def func (a,b,c): print(a,b,c) args = (1,6,3) func (*args)
The so-called solution package is that when a tuple is passed, each element of a tuple corresponds to a positional parameter. Use * When calling Func to remind Python that I want to split args into three separate elements, passed to A,b,c.
Accordingly, there is also a solution package for the dictionary, using the same Func definition, and then:
def func (b): print(b) dict={' A ': 1, ' B ': 2}func (**dict)
Note: Be aware of the distinction between when defined and when it is called. Wrapping and wrapping is not the opposite, it is a two relatively independent process.
"Python" study note 13: Parameter Correspondence of functions