99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Python學(xué)習(xí)之參數(shù)傳遞方式

發(fā)布時(shí)間:2017-05-05 21:14  回復(fù):0  查看:2146   最后回復(fù):2017-05-05 21:14  

本文和大家分享的主要是python中參數(shù)傳遞方式相關(guān)內(nèi)容,一起來看看吧,希望對(duì)大家學(xué)習(xí)python有所幫助。

  位置參數(shù)

  調(diào)用函數(shù)時(shí),根據(jù)函數(shù)定義的參數(shù)位置來傳遞參數(shù)。

  1 def right_triangle_area(a,b):2     return 1/2*a*b3 4 print(right_triangle_area(3,4))5 # 位置參數(shù)傳遞

  求直角三角形面積,a、b分別為兩條直角邊,這里調(diào)用函數(shù)時(shí)使用的是位置參數(shù)傳遞。在位置參數(shù)傳遞中,參數(shù)的順序是不可改變的。

  關(guān)鍵詞參數(shù)傳遞

  在調(diào)用函數(shù)時(shí),通過=的形式加以指定??梢宰尯瘮?shù)更加清晰、容易使用,無需考慮參數(shù)順序。

  1 def right_triangle_area(a,b):2     return 1/2*a*b3 4 print(right_triangle_area(b=4,a=3))5 # 關(guān)鍵詞參數(shù)傳遞

  還有一些類型是默認(rèn)參數(shù)和可變參數(shù)等,目前我暫時(shí)用不到,就不做詳細(xì)分享,有興趣的可以自行百度。

  (二)

  設(shè)計(jì)自己的函數(shù)

  之前介紹了字符串的方法和如何創(chuàng)建函數(shù),這里將前面的學(xué)到的內(nèi)容整合起來,設(shè)計(jì)一個(gè)簡易的敏感詞過濾器。

  1. 傳入?yún)?shù)name(文件名)和msg(信息內(nèi)容)就可以在桌面寫入文件名稱和內(nèi)容的函數(shù)text_create,如果桌面沒有這個(gè)可以寫入的文件時(shí),會(huì)創(chuàng)建一個(gè)再寫入。

  1 def text_create(name,msg):

  2     # 創(chuàng)建文件,寫入信息

  3     desktop_path = '/Users/duwangdan/Desktop/'

  4     # 桌面路徑

  5     full_path = desktop_path + name + '.txt'

  6     # 桌面路徑+文件名+文件后綴

  7     file = open(full_path,'w')

  8     # 'w'參數(shù)指寫入

9     file.write(msg)

10     # 文件中寫入信息

11     file.close()

12     # 寫入后關(guān)閉文件

  在上一篇 《產(chǎn)品經(jīng)理學(xué)Python:學(xué)會(huì)創(chuàng)建并調(diào)用函數(shù)》 中提到,定義函數(shù)后需要return返回結(jié)果。在Python中,return是可選項(xiàng),沒有return也可以直接定義函數(shù)并順利調(diào)用,當(dāng)不寫時(shí),代表返回值是‘None’。

  這時(shí)敏感詞過濾器的第一部分已完成。

  2. 定義一個(gè)名為text_filter的函數(shù),傳入?yún)?shù)word,cencored_word(敏感詞)和changed_word(替換詞),cencored_word默認(rèn)給定‘Awesome’,用changed_word默認(rèn)空值來替代,實(shí)現(xiàn)敏感詞過濾。

1 def text_filter(word,censored_word='Awesome',change_word=''):

2     # 文本過濾函數(shù)

3     return word.replace(censored_word,change_word)

4     # replace()方法替換敏感詞

  3. 定義一個(gè)名為censored_text_create的函數(shù),傳入?yún)?shù)name(文件名),msg(信息),使用第2個(gè)函數(shù)text_filter,將傳入的msg過濾后儲(chǔ)存在clean_msg中,再將傳入的name和過濾好的clean_msg作為參數(shù)傳入text_create函數(shù)中,調(diào)用censored_text_create函數(shù),可以得到過濾后的文本。

1 def censored_text_create(name,msg):

2     # 創(chuàng)建刪除敏感詞后的文本函數(shù)

3     clean_msg = text_filter(msg)

4     # 過濾掉msg中的敏感詞

5     text_create(name,clean_msg)

6     # 傳入nameclean_msgtext_create函數(shù)中

7

8 censored_text_create('test','Math is Awesome!')

9 # 調(diào)用函數(shù)

  完成以上三步后,我們可以得到自己設(shè)計(jì)的文本過濾器了。

  完整代碼如下:

  1 def text_create(name,msg):

  2     desktop_path = '/Users/duwangdan/Desktop/'

  3     full_path = desktop_path + name + '.txt'

  4     file = open(full_path,'w')

  5     file.write(msg)

  6     file.close()

  7

  8

9 def text_filter(word,censored_word='Awesome',change_word=''):

10     return word.replace(censored_word,change_word)

11

12

13 def censored_text_create(name,msg):

14     clean_msg = text_filter(msg)

15     text_create(name,clean_msg)

16

17 censored_text_create('test','Math is Awesome!')

 

來源:博客園

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?