Keyword parameters in Python and non-keyword parameters (variable parameters)

Source: Internet
Author: User
Tags in python

Students who have studied PHP or other languages know that the parameters in PHP are not variable (just a lot of times they can be omitted, because the default values are set for parameters when the function is defined). But that's not true in Python, where Python runs the appearance of variable parameters, which appear in the form of (*ARG,**ARG2).

Let's take a look at this usage today:

For example:

def foo1 (arg1,arg2,key1=1,key2=2,*arg,**keywords):

Print "Arg1 parameters is", arg1

Print "Arg2 parameters is", arg2

Print "Key1 parameter is", Key1

Print "Key2 parameter is", Key2

Print "Arbitrary parameter is", ARG

Print "keywords parameter is", keywords

Foo1 (1,2,3,4,5,6,k1=1,k2=2,k3=3)

Output:

Arg1 parameters is 1

Arg2 parameters is 2

Key1 parameter is 3

Key2 parameter is 4

Arg parameter is (5, 6)

Keywords parameter is {' K3 ': 3, ' K2 ': 2, ' K1 ': 1}

Function parameters are divided into four parts:

Arg1,arg2,key1,key2 General Parameters

*arg non-keyword parameter list

**keywords keyword parameter list

Function reputation part, the four parts of the parameter can not be reversed position, may not have a few parts.

This feature of the Python function makes the function parameters more flexible, and the number of parameters is unrestricted.

Note: This usage is commonly used in the Python adorner, and as for what is an adorner, it is a very important feature in Python and I will explain it later

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.