函數與函數式編程1.介紹: 在過去的十年間,你們廣爲熟知的編程方法無非兩種:面向對象和麪向過程,其實,不管哪一種,都是一種編程的規範或者是如何編程的方法論。 而現在,一種更爲古老的編程方式:函數式編程,以其不保存狀態,不修改變量等特性從新進入人們的視野。 下面咱們就來依次瞭解這一傳統的編程理念,讓咱們從基本的函數概念開始。2.函數定義: 初中數學函數定義: 通常的,在一個變化過程當中,若是有兩個變量x和y,而且對於 x 的每個肯定的值,y 都有惟一肯定的值與其對應, 那麼咱們就把 x 稱爲自變量,把 y 稱爲因變量,y 是 x 的函數。自變量 x 的取值範圍叫作這個函數的定義域 編程語言中函數定義: 函數是邏輯結構化和過程化的一種編程方法。 python 中函數定義方法:def test(x):"The function definitions"x+=1return xdef:定義函數的關鍵字 test:函數名 ():內可定義形參"":文檔描述(非必要,可是強烈建議爲你的函數添加描述信息) x+=1: 泛指代碼塊或程序處理邏輯return:定義返回值 補充: 函數式編程就是:先定義一個數字函數,而後按照這個數學模型用編程語言去實現它。至於具體如何實現和這麼作的好處,後續會詳細介紹
1.面向對象:類 -----》class2.面向過程:過程 ---》def3.函數式編程:函數 ---》defy = 2x 函數把邏輯結構化過程化的一種方法# 定義函數def func1(): """testing1""" print('in the func1') return 0# 定義過程def func2(): '''testing2''' print('in the func2')# 這是定義了一個過程 # 調用函數,x接收返回值0x=func1()# 調用過程,沒有接收返回值,按理說,應該返回什麼?y=func2()# 過程就是沒有返回值的函數print('from func1 return is %s' %x)print('from func2 return is %s' %y)---> in the func1---> in the func2---> from func1 return is 0---> from func2 return is None# 在沒有返回值的狀況下,Python的解釋器隱式的返回了 None,在Python當中,過程也沒當成函數了# 不是說有 return 就叫函數式編程了
3. 爲什麼使用函數 沒有函數的編程只是在寫邏輯(功能),想脫離函數,重用你的邏輯,惟一的辦法就是拷貝 例一: 假設咱們編寫好了一個邏輯(功能),用來以追加的方式寫日誌: with open('a.txt','ab') as f: f.write('end action') 如今有三個函數,每一個函數在處理完本身的邏輯後,都須要使用上面這個邏輯,那麼惟一的方法就是,拷貝三次這段邏輯def test1():print 'test1 starting action...'with open('a.txt','ab') as f: f.write('end action')def test2():print 'test2 starting action...'with open('a.txt','ab') as f: f.write('end action')def test3():print 'test3 starting action...'with open('a.txt','ab') as f: f.write('end action') 那麼假設有 > N 個函數都須要使用這段邏輯,你能夠拷貝 N 次嗎? 例二: 優化後的代碼def logger_test(): with open('a.txt','ab') as f: f.write('end action') 複製代碼
1 >>with open('a.txt','ab') as f: f.write('end action')def test1(): print('test1 starting action...') with open('a.txt','ab') as f: f.write('end action')def test2(): print('test2 starting action...') with open('a.txt','ab') as f: f.write('end action')def test3(): print('test3 starting action...') with open('a.txt','ab') as f: f.write('end action')
2 >>def logger(): with open('a.txt','a+') as f: f.write('end action\n')def test1(): print('test1 starting action...') logger()def test2(): print('test2 starting action...') logger()def test3(): print('test3 starting action...') logger() test1() test2() test3()
3 >>import timedef logger(): time_format = '%Y-%m-%d %X' # X 表明小時 分鐘 秒 time_current = time.strftime(time_format) with open('a.txt','a+') as f: f.write('%s end action\n' %time_current)def test1(): print('test1 starting action...') logger()def test2(): print('test2 starting action...') logger()def test3(): print('test3 starting action...') logger() test1() test2() test3()# 函數的第二個好處就是,可擴展,保持一致性例三: 需求變了(讓咱們來爲日誌加上時間吧)import timedef logger_test(): timeformat='%-%m-%d %x'time_current = time.strftime(time _format) with open('a.txt", 'ab') as f:f.write('time %s end action' %time_current)def test1():print('test1 starting action....') logger_test()def test2():print('test2 starting action....') logger_test()def test3():print('test3 starting action...') logger_test() 總結例二和例三可歸納使用函數的三大優勢1.代碼重用2.保持一致性3.可擴展性