Python函數(二)-參數傳遞

來源:互聯網
上載者:User

標籤:.com   utf-8   運行   image   print   bubuko   位置   分享   傳遞   

首先是規定個數的參數傳遞,有兩種傳遞方式

一個是位置參數,還有一個是關鍵字參數

  • 位置參數

根據位置順序來傳遞參數

# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"def test(a,b): #a和b為形參    print(a)    print(b)    return 0test(1,2) #1和2為實參

運行結果

按照位置順序,1賦值給了a,2賦值給了b

  • 關鍵字參數

根據形參的值來傳遞參數

# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"def test(a,b):    print(a)    print(b)    return 0test(b=1,a=2)

運行結果

把1直接賦值給了形參b,把2直接賦值給了形參a

  • 同時使用位置參數和關鍵字參數

同時使用位置參數和關鍵字參數進行傳參的話,位置參數優先於關鍵字參數

位置參數只能在關鍵字參數前面

# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"def test(a,b,c):    print(a)    print(b)    print(c)    return 0test(3,c=1,b=2)

運行結果

位置參數3傳給形參a,關鍵字參數c和b位置順序沒多大關係

Python函數(二)-參數傳遞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.