*args、**kwargs参数组

'''
def test(*args): # *agrs接收的是N个位置参数,不能接受关键字参数,转化成元祖
print(args)

test(1,2,3,4,5,6)
test(*[1,2,4,5,5]) # arge=tuple([1,2,3,4,5])

def test1(x,*args):
print(x)
print(args)

def test2(**kwargs): #接受n个关键字参数,把N个关键字参数转化为字典
print(kwargs)
print(kwargs["name"])
print(kwargs['age'])
print(kwargs["sex"])

test2(name="alex",age=8,sex='F')
#test2(**{'name':'alex',"age":8})

def test3(name,**kwargs):
print(name)
print(kwargs)

test3('alex',age=18,sex="m") #必须是关键参数,不能直接是字符串test3('alex','sddd',sex="m")

def test4(name,age=18,**kwargs):#默认参数写到后面?不可以,参数组要往后方
print(name)
print(age)
print(kwargs)

test4("alex",sex='m',hobby='tesla',age=3) #默认age可以放到后面?可以,可以位置参数,可以关键字参数
'''
'''
def test5(name,age=18,*args,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)

test5("alex",34, sex='m', hobby='tesla')
'''

猜你喜欢

转载自www.cnblogs.com/wzsx/p/9077176.html