Python advanced tutorial: multiple transfer methods of function parameters, python advanced tutorial

Source: Internet
Author: User
Tags unpack

Python advanced tutorial: multiple transfer methods of function parameters, python advanced tutorial

We have been touched by the function parameter (arguments) transfer. At that time, we passed the corresponding parameters based on the location. We will be exposed to more parameter passing methods.

Recall location transfer:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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


Use python function parameters

The parameters of python functions are similar to the form parameters in C ++, and only copying parameters is passed.
The default parameter of the function is saved as a variable and is a global static variable.

Python parameter passing problems

The understanding of the landlord is correct.
In python, the real parameter passing rules for functions are:
If the parameter name is marked, it must be passed by parameter name. If the order is disordered, the parameter name must be added. Otherwise, the parameter name may be messy.
If there is no default real parameter, it will be passed in sequence. If not, the subsequent values will automatically retrieve their own default values.
If the number of real parameters is greater than that of the form parameter, the parameter name with the * sign is required.

Related Article

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.