Python's *args and **kwargs

Source: Internet
Author: User

When the parameters of a Python function are indeterminate, you can use *args and **kwargs to refer to an indefinite number of arguments.

The difference between the two is that *args is a tuple (tuple), and **kwargs is a dict (dictionary). To put it simply, it is *args without Key,**kwargs with key.

First verify that the above conclusions are correct by code:

def Func_args (*args):    print(type (args))def Func_kwargs (* *  Kwargs):    print(type (Kwargs))if__name__'__main __':    Func_args (1, 2, 3)    Func_kwargs (a=1, b=2, c=3)

Running results, you can clearly see that args is a tuple, Kwargs is a dictionary

<class'tuple'><class'dict' >

Then try to use the *args, implement a function, and return the value of the passed parameter X2. Because the number of arguments passed in is variable, the result returned by the function is also variable.

 def  func_a (*args):  return  Tuple (ARG * 2 for  arg in  Span style= "color: #000000;" > args)  if  __name__  = =  '   

In Func_a, a tuple is returned, and the values of the tuple are assigned to variables A and B, respectively, using the function of the Python tuple to automatically unpack the packets.

Operation Result:

2 4 AA bb

Python's *args and **kwargs

Related Article

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.