python *与**
本文内容纲要:
python中,在形参前面加上“*”与“”“**”,称为动态参数
加“*”时,函数可接受任意多个参数,全部放入一个元祖中
defF(*args):
print(args)
F(123,"456")
#(123,'456')
加“**”时,函数接受参数时,返回为字典,需要写为如下形式:
defF(**kwargs):
print(kwargs)
F(k1=123,k2="456")
#{'k1':123,'k2':'456'}
defF(p,*args,**kwargs):
print(p)
print(args)
print(kwargs)
F(11,"abc",[789],k1=123,k2="456")
#11
#('abc',[789])
#{'k1':123,'k2':'456'}
defF(*args):
print(args)
li=[11,22,33,44]
F(li)
F(*li)
#([11,22,33,44],)
#(11,22,33,44)
defF(**kwargs):
print(kwargs)
li={"k1":1,"k2":2}
F(k=li)
F(**li)
#{'k':{'k2':2,'k1':1}}
#{'k2':2,'k1':1}
本文内容总结:
原文链接:https://www.cnblogs.com/omg-hxy/p/9081177.html