python中偏函數(shù)的應(yīng)用

1 簡介

偏函數(shù)在Python 2.5 版本中添加進(jìn)來,是函數(shù)式編程一系列重要改進(jìn)中的一部分。使用偏函數(shù),可以通過有效地“凍結(jié)”那些預(yù)先確定的參數(shù)來緩存函數(shù)參數(shù),然后在運(yùn)行時,當(dāng)獲得需要的剩余參數(shù)后,可以將它們解凍,傳遞到最終的參數(shù)中,從而使用最終確定的所有參數(shù)去調(diào)用函數(shù)。

偏函數(shù)最好的一點是它不只局限于函數(shù)。偏函數(shù)可以用于可調(diào)用對象(任何包括函數(shù)接口的對象),只需要通過使用圓括號即可,包括類、方法或可調(diào)用實例。對于有很多可調(diào)用對象,并且許多調(diào)用都反復(fù)使用相同參數(shù)的情況,使用偏函數(shù)會非常合適。


# 2 路牌提示實例
我們要做以下路牌:
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4225992-684ffb0d675d1d41.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
點擊每一個按鈕會有相應(yīng)的提示信息,每一個按鈕形式上基本相同,可以考慮用偏函數(shù),而不用去單獨實現(xiàn)每一個按鈕。

以下是代碼:

!/usr/bin/env python

coding=utf-8

import pdb
from functools import partial as pto
from Tkinter import Tk, Button, X
from tkMessageBox import showinfo, showwarning, showerror

WARN = 'warn'
CRIT = 'crit'
REGU = 'regu'

SIGNS = {
'do not enter': CRIT,
'railroad crossing': WARN,
'55\nspeed limit': REGU,
'wrong way':CRIT,
'merging traffic':WARN,
'one way': REGU,
}

critCB = lambda: showerror('Error','Error Button Pressed!')
warnCB = lambda: showwarning('warning','Warning Button Pressed!')
infoCB = lambda: showinfo('Info','Info Button Pressed')

top = Tk()
top.title('Road Signs')
Button(top, text='QUIT',command=top.quit,bg='red',fg='white').pack()

MyButton = pto(Button,top)
CritButton = pto(MyButton, command=critCB, bg='white',fg='red')
WarnButton = pto(MyButton, command=warnCB, bg='goldenrod1')
ReguButton = pto(MyButton, command=infoCB, bg='white')

for eachSign in SIGNS:
pdb.set_trace()
signType = SIGNS[eachSign]
cmd = '%sButton(text=%r%s).pack(fill=X,expand=True)'%
(signType.title(),eachSign,'.upper()' if signType==CRIT else '.title()')
eval(cmd)

top.mainloop()

## 2.1 搞清楚`title()`,`upper()`函數(shù)的作用
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4225992-362c0238f1b23866.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
>str.title() 
Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.
The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
`>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
`

## 2.2 三元組選擇功能以及`%r`和`%s`的區(qū)別
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4225992-aa9cc404c42f25d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

注意其中`%r`的使用,如果換成`%s`會出錯:

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4225992-4a6eac316e84bc2b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

同時,可以看到`text=%r%s`的值為`eachSign.upper()`或者`eachSign.title()`,由`signType`是否為`CRIT`決定。


# 3 運(yùn)行結(jié)果
注意不同類別的按鈕,顯示的界面不太相同,字體是否全為大寫也不一樣。這都是偏函數(shù)的應(yīng)用。
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4225992-684ffb0d675d1d41.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容