Python function asterisk parameter detailed

Source: Internet
Author: User
function in the definition, by the parameter before the asterisking number, passing in a number of arguments into an object, a tuple or a dictionary, it can be said that these parameter values are collected.

Parameter with an asterisk indicates that all values are placed in the same tuple, and the return value of the parameter is a tuple.

The parameter is added with two asterisks to indicate that all values are placed in the same dictionary, and the return value of the parameter is a dictionary.

>>> def print_param (x, y, z = 3, *pospar, **keypar):    print x, y, z    print pospar    print Keypar   >& Gt;> Print_param (3,4,5,6,7,8,m = 1,n = 2) 3 4 5 (6, 7, 8) {' m ': 1, ' n ': 2}

Assigning parameters

function in the call, by the parameter before the asterisking number, the passed parameter must be a tuple or dictionary, the ability to convert its value to the value of the corresponding variable, this process can be considered as the allocation of parameter values.

An asterisk is added to the parameter to indicate that the tuple value is assigned to the corresponding function parameter value.

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

>>> def add (x, y):    print x, y    return x + y   >>> Add (*param) 1%   >>> def test ( Name,age):    print name,age   >>> m = {' name ': ' Xiaoli ', ' age ': '}>>> 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.