Python function parameter Type summary __ function

Source: Internet
Author: User

Positional parameters, default parameters, variable parameters, named keyword parameters, keyword parameters


When used in combination, the definition of a parameter needs to be defined in turn in the direction described above.


The variable parameter actually receives a tuple, and the keyword parameter actually receives a dict.


Define the variable parameters with *, and define the keyword parameters with * *.


The named keyword parameter needs to be separated by the delimiter *, and the following parameter is the named keyword parameter. If you have previously defined a variable parameter, you do not need a separator *



Python's functions have a very flexible parameter shape that enables both simple calls and very complex parameters.

The default parameter must be an immutable object, and if it is a mutable object, the program will run with a logical error.

Note The syntax for defining variable parameters and keyword parameters:

*args is a variable parameter, args receives a tuple;

**KW is the keyword parameter, and kw receives a dict.

And how to pass the syntax of a variable parameter and keyword parameter when calling a function:

Variable parameters can be directly passed in: Func (1, 2, 3), can be assembled first list or tuple, and then passed through the *args: Func (* (1, 2, 3));

Keyword parameters can be passed directly into: Func (A=1, b=2), can be assembled dict first, and then passed **kw to: func (**{' a ': 1, ' B ': 2}).

Using *args and **kw is a Python idiom, and of course you can use other parameter names, but it's best to use idiomatic usage.

Named keyword parameters are designed to limit the number of parameter names that callers can pass in, and can provide default values.

Define a named keyword parameter do not forget to write the separator when there is no variable parameter, otherwise the location parameter will be defined.


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.