Python Indeterminate parameter function

Source: Internet
Author: User

1. Tuple form
def test1 (*args): Print (    ' ############### #test1 ################ ') print (    type (args))    print (args)

Call correctly:

Test1 (1, 2) #args在函数体内部为tuple类型

Error Call:

Test1 (1, b=2) #TypeError: Test1 () got an unexpected keyword argument ' b '
Test1 (A=1, b=2) #TypeError: Test1 () got an unexpected keyword argument ' a '
Test1 (A=1, 2) #TypeError: Test1 () got an unexpected keyword argument ' a '

2. Dictionary form
def test2 (**kargs): Print (    ' ############### #test2 ################ ') print (    type (Kargs))    print (Kargs)

Correct adjustment :

Test2 (A=1, b=2) #kargs在函数体内部为dict类型

Error Call:

Test2 (1, 2) #TypeError: Test2 () takes exactly 0 arguments (2 given)
Test2 (1, b=2) #TypeError: Test2 () takes exactly 0 arguments (2 given)
Test2 (A=1, 2) #SyntaxError: non-keyword arg after keyword arg

3. Mixed form
def test3 (*args, **kargs): Print (    ' ############### #test3 ################ ') print (    type (args))    print ( args) print (    type (Kargs))    print (Kargs

Call correctly:

Test3 (1, 2) #args在函数体内部为tuple类型, Kargs is an empty dict type
Test3 (1, b=2) #args在函数体内部为tuple类型, Kargs for dict type
Test3 (A=1, b=2) #args在函数体内部为空tuple类型, Kargs to Dict type

Error Call:

Test3 (A=1, 2) #SyntaxError: non-keyword arg after keyword arg

Practice:

1 defTest (num,*args,**Kwargs):2     Print('---test1---%d'%num)3     Print('---test2---', args)4     Print('---test3---', Kwargs)5 6Test (100)7Test (100,200)8Test (100,200,300,MM=100)

Answer:

 1  c:\>python test.py   2 ---test1---100 3 --- Test2--- ()   4 ---test3--- {}   5   6 --- Test1---100 7 ---test2---(200  8 ---test3--- {}   9  10 ---test1---10011 ---test2---( 300)  12 ---test3---{"  mm   ' : ' 

Python Indeterminate parameter function

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.