Partial Function Example

Posted tmhm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Partial Function Example相关的知识,希望对你有一定的参考价值。

from functools import partial as pto
from tkinter import Tk, Button, X
from tkinter.messagebox import showinfo, showwarning, showerror

WARN = ‘warn‘
CRIT = ‘crit‘
REGU = ‘regu‘

SIGNS = {
    ‘do not enter‘: CRIT,
    ‘railroad crossing‘: WARN,
    ‘55
speed linit‘: 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‘)
top.geometry(‘250x300‘)
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:
    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()
    

技术分享图片

以上是关于Partial Function Example的主要内容,如果未能解决你的问题,请参考以下文章

Python 偏函数 partial function

python 的偏函数(Partial function)

python 的偏函数(Partial function)

Scala之偏函数Partial Function

Scala之偏函数Partial Function

Scala之偏函数Partial Function