Python函数的不定长参数

需要一个函数能处理比当初声明时更多的参数,这些参数叫做不定长参数,声明时不会对这些参数命名。

‘*’以元组形式输出多变量形式的参数

>>>def employee(*args):           #*args后可接别的参数
        print(type(args))
        print(args)

   #多变量参数people1,people2
>>>people1='Tom';people2='July'
>>>employee(people1,people2)
<class 'tuple'>
('Tom', 'July')

   #多变量参数'xiaoming','lilei'
>>>employee('xiaoming','lilei')
<class 'tuple'>
('xiaoming', 'lilei')

‘ ** ’以字典形式输出多变量形式的参数

>>>def employeed(**args):
        print(type(args))
        print(args)

   #多变量参数Newton=42,Euler=30
>>>employeed(Newton=42,Euler=30)
<class 'dict'>
{'Euler': 30, 'Newton': 42}
>>>def groupp(x,y=1,*people,**name):  
        print(x,y,people,name)    #**name后不能接变量                    
#test
>>>groupp(1,'Tom','Jane',July=42)
1 Tom ('Jane',) {'July': 42}

>>>groupp(2,3,4,5,6)
2 3 (4, 5, 6) {}

>>>groupp(30,July=42)
30 1 () {'July': 42}

你可能感兴趣的:(Python)