Python functions--in formal parameters: *args and **kwargs

Source: Internet
Author: User

Multiple arguments, put in a tuple, with * Start, you can pass multiple parameters, * * is a parameter in accordance with the value of the key to the values of the value of the *args in the dictionary to render the redundant values: (indicates that the arguments in accordance with the location of the value, the more values are given to args, and presented in Ganso Way)

Example:

def foo (x, *args):    Print(x)    print(args) foo (1, 2, 3, 4 , 5) #其中的2, 3,4,5 all to args.

Execution Result:

1(2, 3, 4, 5)

When args is mixed with positional parameters and default parameters: (Note the order of the three)

Example one, (three order is: positional parameter, default parameter, *args)

def foo (x, Y=1, *args):    Print(x)    print(y)     Print (args) foo (1, 2, 3, 4, 5) # where x is the value of 1,y=1 is reset by 2, 3,4,5 has given args

The execution results are:

(3, 4, 5)

Example two, (three order is: position parameter, *args, default parameter)

EF foo (x,*args,y=1):    print(x)    print(args)    Print  (y) foo (1,2,3,4,5)# where x is 1,2,3,4,5 all gives Args,y the default parameter is still 1

The execution results are:

1(2, 3, 4, 5)1

About *, can be viewed from 2 points of view (need to split):

1, from the perspective of formal parameters:

Example:

def foo (*args):# in fact, this action is equivalent to Def foo (a,b,c,d,e):    print(args) foo ( 1,2,3,4,5)# where the 1,2,3,4,5 are passed to the a,b,c,d,e according to the location value respectively

The execution results are:

(1, 2, 3, 4, 5)

2, from the point of view of the actual argument:

Example:

def foo (x, y    , z): Print (x)     Print (y)     Print (z)     foo (* (+))# where * (three-way) is taken apart, Foo (three-way) is passed to X, Y and Z according to the location value.

The execution results are:

123

**kwargs: (indicates that the value of the parameter is presented in a dictionary by the values of the key)

Example:

def foo (x,**Kwargs):    Print(x)    print(Kwargs) foo (1, y=1,a=2,b=3,c=4)# y=1,a=2,b=3,c=4 to the Kwargs in a dictionary

The execution results are:

1(2, 3, 4) {'y'a' a 'b ' ' C ': 4}

Error Example: (due to sequential error)

EF foo (x,**kwargs,*args):    Print(x)    print(args)      Print(Kwargs) foo (1,y=1,a=2,b=3,c=4,2,3,4)

The result of the execution will be an error:

Syntaxerror:invalid syntax

Position parameters, default parameters, **kwargs the order must be positional parameters, default parameters, **kwargs, or will be error:

Example:

def foo (x,y=1,**Kwargs):    Print(x)    print(y)     Print (Kwargs) foo (1,a=2,b=3,c=4) # Send 1 by location to X, y by default parameter is 1,a=2,b=3,c=4 to Kwargs in dictionary mode

The execution results are:

{'a'b'c': 4}

About * *, can be viewed from 2 points of view (need to split):

1, from the perspective of formal parameters:

Example:

def foo (**kwargs):# is actually equivalent to Def foo (y,a,b,c)    print(Kwargs) foo (y=1 , a=2,b=3,c=4)

The execution results are:

{'y'a'a 'b'C  ': 4}

2, from the point of view of the actual argument:

Example one:

deffoo (a,b,c,d):Print(a)Print(b)Print(c)Print(d) Foo (**{"a": 2,"b": 3,"C": 4,"D": 5})#**{"A": 2, "B": 3, "C": 4, "D": 5} is passed to a,b,c,d for each value in the dictionary as a keyword

Execution results are

2345

Example two:

 def  foo (a,b,c,d=1 print   (a)  print  Span style= "COLOR: #000000" > (b)  print   (c) /span>print   (d) Foo ( **{"  a   ": 2,"  b   ": 3, " c  " : 4}) #  **{"a": 2, "B": 3, "C": 4} is passed the value of each value in the dictionary to the A,b,c ; d still follows the default parameter  

The execution results are:

2341

Python functions--in formal parameters: *args and **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.