python 函數參數

來源:互聯網
上載者:User

標籤:函數名   完全   函數   ber   number   other   調用   numbers   必須   

 

函數名其實就是指向一個函數對象的引用,完全可以把函數名賦給一個變數,相當於給這個函數起了一個“別名”:

>>> a = abs # 變數a指向abs函數>>> a(-1) # 所以也可以通過a調用abs函數1

 

定義函數:

def my_abs(x):    if x >= 0:        return x    else:        return -x

函數體內部的語句在執行時,一旦執行到return時,函數就執行完畢,並將結果返回。

如果沒有return語句,函數執行完畢後也會返回結果,只是結果為None。

return None可以簡寫為return。

 

函數的參數:

1.位置參數:

def power(x):    return x * x

2.預設參數:

def power(x, n=2):    s = 1    while n > 0:        n = n - 1        s = s * x    return s

3.可變參數:

def calc(*numbers):    sum = 0    for n in numbers:        sum = sum + n * n    return sum

4.關鍵字參數:

def person(name, age, **kw):    print(‘name:‘, name, ‘age:‘, age, ‘other:‘, kw)

  

參數定義的順序必須是:必選參數、預設參數、可變參數、命名關鍵字參數和關鍵字參數

 

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.