Python Basics: 11.2_ function calls

Source: Internet
Author: User

We have been exposed to the functions (function) parameters (arguments) passed . At that time we passed the corresponding parameters according to the location . The way this parameter is passed is passed as the position of the function parameter .

We will be exposed to more parameter passing methods.

Recall the location pass:

def f (a,b,c):    return a+b+cres = f (All-in-all) print res

When the F function 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:

res = f (c=3, b=2, a=1) Print res

Keyword delivery can be mixed with location passing. However, the positional parameters are preceded by the keyword parameter:

res = f (1, c=3, b=2) Print res

If you do not follow the above rules when mixing keyword parameters and positional parameters, you will get an error:non-keyword arg after keyword arg

res = f (a=1, 2, c=3) Print res

Default parameters

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.

def f (a,b,c=10):    return a+b+cres = f (3,2) Print resres = f (3,2,1) Print res

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.

There is one more rule: when defining a function, you must put the parameter with the default value behind it.

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.
Parcel delivery is delivered in two ways: through a tuple package or through a dictionary package .

(1) Meta-Group Package

Here is an example of a tuple Parcel Location pass :

def func (*name):    print type (name)    print Namefunc (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 .

In order 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 .

(2) Dictionary Package

Here is an example of a dictionary wrap keyword pass:

def func (**dict):    print type (dict)    print Dictfunc (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).

(1) Tuples are unpacked by location

Here is an example:

def func (a,b,c):    Print A,b,cargs = (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). )

If you don't need to unpack, the function call will have the type error of the function parameter.

(2) Dictionary wrapped by key word

Accordingly, there is also 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, where the argument is passed as a keyword parameter, that is, the parameter of Func is A,b,c, then the key of the Dict dictionary is not the same as a,b,c. (The order of the A,b,c is unimportant).

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: The first position, then the key word, then wrap the position, and then wrap the key words, and according to the principle of the above carefully distinguish.

Note: be aware of the distinction between function definitions and when function calls. Wrapping and wrapping is not the opposite, it is a two relatively independent process.

Python Basics: 11.2_ function calls

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.