Python function (ii)-parameter passing

Source: Internet
Author: User

The first is to specify the number of parameters to pass, there are two ways to transfer

One is the positional parameter, and the other is the keyword parameter

    • Position parameters

Passing parameters according to positional order

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" def Test (a B): #a和b为形参    Print (a)    print (b)    return 0test #1和2为实参

Run results

In order of position, 1 was assigned to the a,2 assignment to B.

    • Keyword parameters

Passing parameters according to the value of the formal parameter

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" def Test (a):    print (a)    print (b)    return 0test (b=1,a=2)

Run results

The 1 is directly assigned to the parameter B, and the 2 is directly assigned to the parameter a

    • Use both positional and keyword parameters

The positional parameter takes precedence over the keyword parameter when using the positional parameter and the keyword parameter to pass the parameter.

Positional parameters can only be preceded by the keyword parameter

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" def Test (a,b,c):    print (a)    print (b)    print (c)    return 0test (3,c=1,b=2)

Run results

Position parameter 3 to the parameter A, the keyword parameters C and b position order does not matter how much

Python function (ii)-parameter passing

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.