位置:首頁(yè) > 軟件操作教程 > 編程開(kāi)發(fā) > Python > 問(wèn)題詳情

python 關(guān)鍵參數(shù)

提問(wèn)人:楊紫紅發(fā)布時(shí)間:2020-11-20

如果某個(gè)函數(shù)有很多參數(shù),而現(xiàn)在只想指定其中的部分,那么可以通過(guò)命名為這些參數(shù)賦值(稱為‘關(guān)鍵參數(shù)’)。

優(yōu)點(diǎn):不必?fù)?dān)心參數(shù)的順序,使函數(shù)變的更加簡(jiǎn)單;假設(shè)其他參數(shù)都有默認(rèn)值,可以只給我們想要的那些參數(shù)賦值。

def func(a, b=2, c=3):

    print "a is %s, b is %s, c is %s" % (a, b, c)


func(1) #a is 1, b is 2, c is 3

func(1, 5) #a is 1, b is 5, c is 3

func(1, c = 10) #a is 1, b is 2, c is 10

func(c = 20, a = 30) #a is 30, b is 2, c is 20

繼續(xù)查找其他問(wèn)題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部