我正在像下面这样在空闲状态下进行测试,但是出了点问题..
>>> print 1<>1
False OK
>>> print 1<>0
True OK
>>> print False | True
True OK
>>> print False | 1<>0
True OK
>>> print 1<>1 | 1<>0
False WHY ???
请您参考如下方法:
您正在使用 | 按位或 运算符。您可能正在寻找 or运算符代替。 or是 bool OR 运算符。
请注意 <>已弃用,您想使用 !=真的。
>>> 1 != 1
False
>>> 1 != 0
True
>>> False or True
True
>>> False or 1 != 0
True
>>> 1 != 1 or 1 != 0
True
在 Python 中,bool() type 是 int() 的子类, 与 True等于 1 和 False等于 0,因此在 True 上使用按位运算符和 False具有相同的输出,就好像这些值是整数一样:
>>> True == 1
True
>>> 10 | True
11
但是因为|比 != 有一个更高的先例(或 <> )它首先被执行:
>>> 1 != (1 | 1) != 0
False
>>> (1 != 1) | (1 != 0)
True
or 短路,它返回第一个 bool 值 true-ish 的表达式值(对于集合非空,对于数值非零)。
