Maorongrong smile like sunshine

and - or技巧in Python

2016-04-06

and - or in Python.

介绍一个python中的小技巧:and-or

a = "heaven"
b = "hell"
c = True and a or b
print c
d = False and a or b
print d

输出:
heaven
hell

抛开绕人的and和or的逻辑,

你只需记住, 在一个bool and a or b语句中,当bool条件为真时,结果是a;当bool条件为假时,结果是b

if a > 0:
print "big"
else:
print "small"

就可以直接写成:
print (a > 0) and "big" or "small"

and or语句是利用了python中的逻辑运算实现的。当a本身是个假值(如0,"")时,结果就不会像你期望的那样

a = ""
b = "hell"
c = True and a or b
print c

得到的结果不是""而是"hell"。因为""和"hell"做or的结果是"hell"

## and-or真正的技巧

在于确保a的值不会为假。 最常用的方式是使 a 成为 [a] 、 b 成为 [b],然后使用返回值列表的第一个元素:

a = ""
b = "hell"
c = (True and [a] or [b])[0]
print c

由于[a]是一个非空列表,所以它决不会为假。即使a是0或者’‘或者其它假值,列表[a]也为真,因为它有一个元素。

在两个常量值进行选择时,and-or会让你的代码更简单。但如果你觉得这个技巧带来的副作用已经让你头大了,没关系,用if-else可以做相同的事情。不过在python的某些情况下,你可能没法使用if语句,比如lambda函数中,这时候你可能就需要and-or的帮助了。


Comments