[python][oldboy]關鍵字參數和位置參數,預設參數,可變長參數(無名,有名)

來源:互聯網
上載者:User

標籤:proc   python   splay   war   return   lis   div   **kwargs   一個   

關鍵字參數和位置參數是在函數調用的時候定義

  關鍵字參數是以鍵-值對出現,無序,多一個不行,少一個不行

  位置參數,有序,多一個不行,少一個不行

  混搭:所有位置參數必須在關鍵字參數的左邊

  

預設參數是在函數定義的定義,在函數調用的時候可以不用傳改參數

 

可變長參數:*args, **kwargs

 

可變長參數和位置參數的混搭

 1 def add(x,y): 2     return x + y 3 def add_1(x=0, y=1): 4     return x + y 5  6 def list_add(x, *args, **kwargs): 7     print x 8     print args 9     print kwargs10 11 print "function 位置參數%d" % add(1,3)12 print "function 關鍵字參數: %d" % add(x=2, y=4)13 print "function 預設參數 %d" % add_1()14 print "function 位置關鍵字混搭 %d" % add(0, y=2)15 list_add(1, 2, 3, 4, name=1)16 list_add(1,*[2,3,4],**{"name":1})
View Code

 

 1 function 位置參數4 2 function 關鍵字參數: 6 3 function 預設參數 1 4 function 位置關鍵字混搭 2 5 1 6 (2, 3, 4) 7 {‘name‘: 1} 8 1 9 (2, 3, 4)10 {‘name‘: 1}11 12 Process finished with exit code 0
View Code

 

[python][oldboy]關鍵字參數和位置參數,預設參數,可變長參數(無名,有名)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.