python 關鍵參數(shù)
提問人:楊紫紅發(fā)布時間:2020-11-20
如果某個函數(shù)有很多參數(shù),而現(xiàn)在只想指定其中的部分,那么可以通過命名為這些參數(shù)賦值(稱為‘關鍵參數(shù)’)。
優(yōu)點:不必擔心參數(shù)的順序,使函數(shù)變的更加簡單;假設其他參數(shù)都有默認值,可以只給我們想要的那些參數(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ù)查找其他問題的答案?
相關視頻回答
點擊加載更多評論>>