Python function parameters multiple delivery methods

Source: Internet
Author: User

Multiple transfer methods of function parameters in Python advanced tutorial

We have been exposed to the functions (function) parameters (arguments) passed. At that time we passed the corresponding parameters according to the location. We will be exposed to more parameter passing methods.
Recall the location pass:
Copy the code code as follows:

def f (a,b,c):
Return A+b+c
Print (f (+/-))

When F is called, the three-a,b,c is passed to the location, respectively.
Keyword delivery
In some cases, passing the position will feel more rigid. The keyword (keyword) pass is a parameter that is passed according to the name of each parameter. Keywords do not follow the correspondence of location. Still use the definition of the above F, change the invocation method:
Copy the code code as follows:

Print (f (c=3,b=2,a=1))
Keyword delivery can be mixed with location passing. However, the positional parameters are preceded by the keyword parameter:
Copy the code code as follows:

Print (f (1,c=3,b=2))
Parameter Default value
When defining a function, you can assign a default value to the parameter by using a form like a=19. If the parameter is not eventually passed, the default value is used.
Copy the code code as follows:

def f (a,b,c=10):
Return A+b+c
Print (f (3,2))
Print (f (3,2,1))

At 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 called, C is assigned a value of 1, and the default value is no longer used.
Parcel delivery
When defining a function, we sometimes don't know how many arguments are passed when the call is called. At this time, the parcel (packing) positional parameter, or the package keyword parameter, can be very useful for parameter passing.
Here is an example of the parcel location pass:
Copy the 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 location, which is the parcel location pass.
To remind the Python parameter, name is the tuple name used to pass the parcel location, and when Func is defined, the * number is added before name.
Here is an example of the delivery of a parcel keyword:
Copy the 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 previous example, Dict is a dictionary that collects all the keywords and passes it to the function func. To remind Python, the parameter dict is the dictionary used to pass the Parcel keyword, plus * * before dict.
The key to parcel delivery is to add * or * * before the corresponding tuple or dictionary when defining a function.
Solution Package
* and * *, can also be used at the time of invocation, that is, the solution package (unpacking), the following example:
Copy the 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, which is to pass a tuple, each element of the 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. (Imagine what happens when you invoke Func without a * in front of args). )
Accordingly, there is also a solution package for the dictionary, using the same Func definition, and then:
Copy the code code as follows:

Dict = {' A ': 1, ' B ': 2, ' C ': 3}
Func (**dict)

When passing the dictionary dict, let each key value pair of the dictionary be passed as a keyword to func.

Mixed
When defining or invoking parameters, several methods of passing parameters can be mixed. But be careful about the sequence in the process. The basic principle is that the first position, then the keyword, then wrap the position, and then wrap the keyword, and according to the above-mentioned principle and carefully distinguish.
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.
Summarize
keyword, default value,
Parcel location, parcel key word
Solution Package

Python function parameters multiple delivery methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.