您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)函數(shù)參數(shù)調(diào)用和非固定參數(shù)的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
def test(x,y,z): #這添加形參
print(x)
print(y)
print(z)
test(1,2,3) # 這里添加實(shí)參
#test(1,2)#位置參數(shù)調(diào)用:實(shí)參與形參一一對(duì)應(yīng),不能多,不能少
#test(y=1,x=2)#關(guān)鍵字 :與形參順序無(wú)關(guān)
#test(y=1,x=2,1)# 關(guān)鍵字參數(shù)不能放在位置參數(shù)前面
#test(1,z=2,y=5)
def test(x,y=2):
print(x)
print(y)
test(1,y=3)
#def test(*gg): #可接受任意數(shù)量實(shí)參,變成元組的形式
print(gg)
test(1,2,3,4,5,5,5,5,5,7,9)
test(*[1,2,3,4,5]) # gg=tuple([1,2,3,4,5])
def test(x,args): # 號(hào)代表功能
print(x)
print(args)
#test(1,2,3,4,5,6,7)
#test([1,2,3,4,5,6,7]) #args=*[1,2,3,4,5,6,7]
def test1(**kwargs):!
print(kwargs)
print(kwargs["name"])
print(kwargs["age"])
test1(name="alex",age=8) #把n個(gè)關(guān)鍵字參數(shù),轉(zhuǎn)換成字典
test1(**{"name":"alex","age":"8"})
#def test3(name,**kwargs):
print(name)
print(kwargs)
test3("alex",age=18,sex="m")
def test3(name,x=2,**kwargs):
print(name)
print(x)
print(kwargs)
test3("alex",age=18,sex="m",x=4)
def test3(name,x=2,*args,**kwargs):
print(name)
print(x)
print(args)
print(kwargs)
test3("alex",age=18,sex="m",x=4)
總結(jié):位置參數(shù)只會(huì)傳給形參和*args,關(guān)鍵字參數(shù)傳給*kwargs!
args會(huì)把n個(gè)位置參數(shù)轉(zhuǎn)換成元祖的形式!
**kwargs會(huì)把n個(gè)關(guān)鍵字參數(shù)轉(zhuǎn)換成字典形式!m=‘a(chǎn)lex’ (m是key,‘a(chǎn)lex’是values)
感謝各位的閱讀!關(guān)于“函數(shù)參數(shù)調(diào)用和非固定參數(shù)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。