Python function parameter *args **kwargs

Source: Internet
Author: User

After graduating for many years, the C + + ate the rice .... Today I'm in the tangle of what formal parameters are and what are arguments ....

The parameter that is written in the definition function is the formal parameter, because there is no memory footprint, the arguments written on the actual call are arguments, because there is memory consumption and value

Then there is the positional parameter, the variable parameter and the keyword parameter

def foo (a,*b,**c):    print(a)    print(b)      Print(c)if__name__= ="__main__":    foo (1,2,3,4,k=1,g=2)

such as the program, a,*b,**c these three are formal parameters, a,*b these two are positional arguments, **c is the keyword parameter, and *b,**c these two are mutable parameters

1,2,3,4,k=1,g=2 These are arguments, 1,2,3,4 are positional parameters; k=1,g=2 is a keyword parameter; 1 assigns a value to a;2,3,4 as a tuple to *b;k=1,g=2 pay **c in dictionary form

The output is like this.

1(2, 3, 4) {'g'k': 1}

So Fun (*args,**kwargs)

*args: (indicates that the value is passed by location in the argument, the extra value is given to args, and is rendered in tuples)

**kwargs: (indicates that the value is presented as a dictionary by keyword)

It is important to note that the keyword content passed by **kwargs can be a value (numeric, list, tuple, dictionary, etc.) and can be a function or a class

Python function parameter *args **kwargs

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.