python基礎—初識函數

來源:互聯網
上載者:User

標籤:使用   提醒   函數   函數名   郵箱   func   mem   指標   UNC   

1.python中函數定義:函數是邏輯結構化和過程結構化的一種變成方法。

python中函數定義方法:

def test(x):

  ‘‘The function definitions‘‘

  x+=1

  return x

 

def :定義函數的關鍵字

test :函數名

():可定義形參

‘’‘’:文檔描述,非必要

x+=1: 泛指代碼塊或程式處理邏輯

return: 定義傳回值

 

2.為什麼要使用函數

背景摘要:現在需要寫一個監控程式,當CPU,MEMORY,DISK等指標的使用量超過閾值時發送警示郵件

while1:    if cpu>90%:        #發送寄件提醒        串連郵箱伺服器        發送郵件        關閉連結        if memory >90%:        #發送寄件提醒        串連郵箱伺服器        發送郵件        關閉連結        if disk >90%        #發送寄件提醒        串連郵箱伺服器        發送郵件        關閉連結    

 

def 發送郵件(內容):
  #發送寄件提醒
  串連郵箱伺服器
  發送郵件
  關閉連結

while 1: if cpu>90%: 發送郵件(‘cpu警示‘) if memory>90%: 發送郵件(‘memory警示‘) if disk>90%: 發送郵件(‘disk警示‘)

  總結使用函數的好處:

  1.代碼重用

  2.保持一致性,容易維護

  3.可擴充性

 

3.函數參數

 

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.