函数参数

#*args:接受N个位置参数,转换成元组形式
def test(*args):
print(args)
test(1,2,3,4,5,5)
test(*[1,2,4,5,5])# args=tuple([1,2,3,4,5])
def test1(x,*args):
print(x)
print(args)
test1(1,2,3,4,5,6)
def test2(**kwargs):#接受N个关键字参数转换成字典形式
print(kwargs)
print(kwargs['name'])
print(kwargs['age'])
test2(name='lian',age=8,sex='f')
test2(**{'name':'lian','age':8})
def test3(name,**kwargs):
print(name)
print(kwargs)
test3('Lian',age=18,sex='m')
def test4(name,age=18,**kwargs):#参数组要在后边
print(name)
print(age)
print(kwargs)
test4('lian',sex='m',hobby='play',age=30)
def logger(source):
print("from %s" %source)
def test5(name,*args,age=18,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)
logger("Test5")
test5('lian',1,2,30,sex='m',hobby='play',age=11)#位置参数要在关键字参数之前



(1, 2, 3, 4, 5, 5)
(1, 2, 4, 5, 5)
1
(2, 3, 4, 5, 6)
{'name': 'lian', 'age': 8, 'sex': 'f'}
lian
8
{'name': 'lian', 'age': 8}
lian
8
Lian
{'age': 18, 'sex': 'm'}
lian
30
{'sex': 'm', 'hobby': 'play'}
lian
11
(1, 2, 30)
{'sex': 'm', 'hobby': 'play'}
from Test5

原文地址:https://www.cnblogs.com/rongye/p/9915331.html