Python function asterisk parameters

Source: Internet
Author: User
In the definition, a function adds a star number before a parameter to convert multiple parameters passed in into an object, tuples, or dictionary, it can be said that these parameter values are collected and the function is defined. by adding a star number before the parameter, the passed parameters are converted into an object, tuples, or dictionary, it can be said that these parameter values are collected.

Adding an asterisk before the parameter indicates that all values are placed in the same tuples. The return value of this parameter is a tuple.

Adding two asterisks before the parameter indicates that all values are placed in the same dictionary, and the return value of this parameter is a dictionary.

>>> Def print_param (x, y, z = 3, * pospar, ** keypar): print x, y, z print pospar print keypar >>> print_param (3, 4, 5, 6, 7, 8, m = 1, n = 2) 3 4 5 (6, 7, 8) {'M': 1, 'n': 2}

Allocation parameters

When a function is called, by adding a star number before the parameter, the transmitted parameter must be a tuples or dictionary, and the value can be converted to the value of the corresponding variable, this process can be seen as the allocation of parameter values.

Add an asterisk before the parameter to indicate that the value of the tuples is allocated to the corresponding function parameter value.

The first two asterisks of the parameter indicate that the value in the dictionary is assigned to the key, and the key must be used as the parameter name in the function definition.

>>> Def add (x, y): print x, y return x + y >>>> add (* param) 1 23 >>> def test (name, age ): print name, age >>> m = {'name': 'Xiaoli', 'age': '12' }>>> test (** m) xiaoli 12 >>> m = {'name': 'Xiaoli', 'age': 12 }>>> test (** m) xiaoli 12

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.