我有这些
def MsgBox1_YesChosen(sender,e):
if e.Key != "A": function0()
else:
function1()
function2()
function3()
def MsgBox1_NoChosen(sender,e):
if e.Key == "A": function0()
else:
function1()
function2()
function3()
两个def可以合并在一起吗?它们之间唯一的区别是“==”,“!=“
Tags:
将comparisson运算符作为参数传递。 您不仅可以传递运算符,还可以传递任何其他函数--但“equal”和“not equal”以及所有其他比较运算符或算术运算符都已在“运算符”模块中定义为正确的函数-您的代码可以变成:
是的,以一种非常普遍的方式-你只需要把你的头脑围绕在以下事实上:(1)函数是一级值,(2)运算符只是具有特殊语法处理的函数。例如:
使用like(在导入^{} -你可以用lambda来完成,但是对于操作符,
operator
模块是更干净的解决方案)MsgBox1_YesChosen = make_handler(operator.ne)
(这是一个可怕的名称btw)。在相关问题 更多 >
编程相关推荐