python--function 8, variable length parameter

Source: Internet
Author: User

Variable length parameter: variable length refers to the number of arguments is not fixed
Variable-length arguments defined by location: *
Variable-length arguments defined by keyword: * *

One, by location
def func (x,y,*args):    Print(x,    y)print(args) func ( 1,2,3,4,5)  #  * Process the extra arguments defined by location  #  and then assign the variable to the * to save it in the form of a tuple   #  args= (3,4,5)
View Code

1. Equivalent

def func (x,y,*args):    Print(x,    y)print(args) func ( # As with func (1,2,3,4,5), why the next principle
View Code

2. Principle

def func (x, y    , z): Print (x, Y, Z) func (* (+) ) # *== position parameter will be opened ( # and then the assignment .
View Code

Second, by keyword

def func (x,y,**Kwargs):    Print(x,    y)print(Kwargs) func ( 1,y=2,z=3,a=1,b=3)  #  * * processing the extra argument defined by the keyword  #  and assigning it to * * After the variable to be saved into a dictionary form  #  kwargs={' z ': 3, ' a ': 1, ' B ': 3}
View Code

1. Equivalent

def func (x,y,**Kwargs):    Print(x,    y)print(Kwargs) func ( 1,y=2,**{'a': 1,'b': 3,'Z  ': 3})# as with func (1,y=2,a=1,b=3,z=3), the principle in the next
View Code

2. Principle

deffunc (x, Y, z):Print(x, Y, Z) func (**{'y': 2,'x': 1,'Z': 3})The **== keyword parameter will {'y': 2,'x': 1,'Z': 3} disassemble#and then the assignment .
View Code



python--function 8, variable length parameter

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.