很多语言种都有这样一个三目运算符,bool ? a : b,使用也很方便,bool为true返回a,否则返回b,python中需要使用and or这两个东东达到同样的效果,实际上python中的逻辑运算符挺让人头疼的。
举个简单的例子:
python 代码
and a or b 'first' >>> 0 and a or b 'second'
>>> a = "first" >>> b = "second" >>> 1 上面的这段代码就是等同三目运算符的了.
and操作会依次比较各个表达式,返回第一个为false的表达式,否则返回最后一个为true的表达式
or操作刚好相反,返回第一个为true的表达式,否则返回最后一个为false的表达式
这样在看上面的代码就容易多了
Example code的脚本中有这么一段
python 代码
- processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)