可变长参数之*
def func(name,pwd,*args): #*args会接受多余位置的实参,args是约定俗称
print('name:',name,'pwd:',pwd)
print(args) #args就会接收多余的位置实参,以元祖的方式存储
return 1
res=func('nick',123658,18,180,140,'a',234) #用户使用func函数的时候会传用户的信息进入函数,但是不知道传几个
print(res)
name: nick pwd: 123658
(18, 180, 140, 'a', 234)
1
可变长参数之**
def func(name,pwd,**kwargs): #kwargs会接受多余位置的关键字实参,kwargs是约定俗成
print('name:',name,'pwd:',pwd)
print(kwargs) #kwargs会接受多余的关键字实参,以字典形式存储
return 1
res=func('nick',123658,age=18,height=180)
print(res)
name: nick pwd: 123658
{'age': 18, 'height': 180}
1
可变长参数*和**连用
def func(name,pwd,*args,**kwargs):
print('name:',name,'pwd:',pwd)
print(args)
print(kwargs)
return 1
res=func('nick',123456,1,4,5,'a','q',age=18,height=180)#kwargs接收的关键字实参要写在末尾
print(res)
了解
def func(name,pwd,*args):
print('name:',name,'pwd:',pwd)
print(args)
return 1
tup=(4,1,3,'a')
res=func('nick',123456,*tup) #*会把元祖打散成位置实参传给形参
print(res)
name: nick pwd: 123456
(4, 1, 3, 'a')
1
def func(name, pwd, **kwargs):
print('name:', name, 'pwd:', pwd)
print(kwargs)
return 1
dic = {'a': 1, 'b': 2} # **dic --> a=1 , b=2
res = func('nick', 123658, **dic) # **会把字典打散成关键字实参传给形参
# res = func('nick', 123658, a=1, b=2) # **会把字典打散成关键字实参传给形参
print(res)