在python lambda函数中使用OR运算符 [英] Use of OR operator in python lambda function
本文介绍了在python lambda函数中使用OR运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
O Reilly编程Python书中有一个代码示例,该代码示例在lambda函数中使用OR运算符.文字指出"[[代码]使用or运算符强制两个表达式运行".
There is a code example in the O Reilly Programming Python book which uses an OR operator in a lambda function. The text states that "[the code] uses an or operator to force two expressions to be run".
这如何以及为什么起作用?
How and why does this work?
widget = Button(None, # but contains just an expression
text='Hello event world',
command=(lambda: print('Hello lambda world') or sys.exit()) )
widget.pack()
widget.mainloop()
推荐答案
Python中的每个函数都返回一个值.如果没有显式的return语句,它将返回None
. None
作为布尔表达式的结果为False
.因此,print
返回None
,并且总是评估or
表达式的右侧.
Every funnction in Python returns a value. If there is no explicit return statement it returns None
. None
as boolean expression evaluates to False
. Thus, print
returns None
, and the right hand side of the or
expression is always evaluated.
这篇关于在python lambda函数中使用OR运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文