The difference between *args and **kwargs in Python

Source: Internet
Author: User

(Note: This article is part of the content from the Internet, due to the author's limited, shortcomings, but also hope to comment.) )

mid-Autumn night, slightly cool, but always can't see the moon.

I think, it must be shy, quietly hide behind the clouds.

Well, that's it, I'm so damn witty.

Text:

Note: The 11.6 variable length parameters of the Python Core programming 2nd edition are described in more detail---chapters.

First, the use of *args method

*args is used to package parameters into a tuple to call the function body

Example one:

def function (*args):    print(args, type (args)) function (1)

The output results are shown in tuples:

Example two:

def function (x, y, *args):    Print(x, y, args) function (1, 2, 3, 4, 5)

Output Result:

Second, the use of **kwargs method

**kwargs Package keyword parameter into dict to function body call

Example one:

def Function (* *Kwargs)    :print(Kwargs, type (Kwargs)) function (a=2)

The output is displayed as a list:

Example two:

def Function (* *Kwargs)    :print(Kwargs) function (a=1, b=2, c=3)

Output Result:

Note: The position of the parameter arg, *args, and **kwargs three parameters must be certain. Must be (Arg,*args,**kwargs) this order, or the program will error.

def function (arg,*args,**kwargs):    print(arg,args,kwargs) function ( 6,7,8,9,a=1, b=2, c=3)

Output Result:

Oops, finished.

Mid-Autumn Festival, think more, want to mention a pot of wine about a few old friends driving a leaf boat,

Lost in the hometown of Jiangnan Water village. The boat swung clear and lakes over the waves.

The difference between *args and **kwargs in Python

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.