Python的在(__contains__)运算符返回一个布尔值,其值非真非假 [英] Python's in (__contains__) operator returns a bool whose value is neither True nor False
问题描述
如所预期的,1不被空的元组包含
As expected, 1 is not contained by the empty tuple
>>> 1 in ()
False
但返回的假
值不等于假
>>> 1 in () == False
False
看着它在运营商的另一种方式,在返回
布尔
既不是真
也不假
:
Looking at it another way, the in
operator returns a bool
which is neither True
nor False
:
>>> type(1 in ())
<type 'bool'>
>>> 1 in () == True, 1 in () == False
(False, False)
然而,如果原来的前pression被括号正常行为恢复
However, normal behaviour resumes if the original expression is parenthesized
>>> (1 in ()) == False
True
或它的值存储在变量
or its value is stored in a variable
>>> value = 1 in ()
>>> value == False
True
此行为在二者的Python 2和Python 3。观察
This behaviour is observed in both Python 2 and Python 3.
你能解释一下这是怎么回事?
Can you explain what is going on?
推荐答案
您正在运行到比较运营商链接;在 1()==假
做的不可以的意思是(1())==假
。
You are running into comparison operator chaining; 1 in () == False
does not mean (1 in ()) == False
.
相反,比较是链接和前pression真正含义是:
Rather, comparisons are chained and the expression really means:
(1 in ()) and (() == False)
由于(1())
已经假的,链接的前pression下半年被完全忽略(因为假和something_else
收益假
任何价值 something_else
会)。
Because (1 in ())
is already false, the second half of the chained expression is ignored altogether (since False and something_else
returns False
whatever the value of something_else
would be).
比较可以任意链接,例如, X&LT; Y'LT; = Z
等同于 X&LT; Y和Y LT; = Z
,但是
只计算一次(但在这两种情况下,以Z
是不是在所有的时候 X'LT。ÿ
被发现是假的)
Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is equivalent tox < y and y <= z
, except thaty
is evaluated only once (but in both casesz
is not evaluated at all whenx < y
is found to be false).
有关的记录,&LT;
,&GT;
, ==
,&GT; =
,&LT; =
, = $! C $ C>,
都是比较操作符(因为是pcated去$ p $ 是
,不是
,在
在&LT;&GT;
)不是。
For the record, <
, >
, ==
, >=
, <=
, !=
, is
, is not
, in
and not in
are all comparison operators (as is the deprecated <>
).
在一般情况下,不比较的布尔值;只是测试除权pression本身。如果你的有无的测试对一个布尔值,至少用括号和是
操作符,真
和假
是单身,就像无
:
In general, don't compare against booleans; just test the expression itself. If you have to test against a boolean literal, at least use parenthesis and the is
operator, True
and False
are singletons, just like None
:
>>> (1 in ()) is False
True
这变得更加混乱时仍然整数都有涉及。 Python的布尔
类型 INT
的子类 1 。因此,假== 0
是真实的,因为是真== 1
。为此,您可以想见创建链接操作,几乎看理智的:
This gets more confusing still when integers are involved. The Python bool
type is a subclass of int
1. As such, False == 0
is true, as is True == 1
. You therefor can conceivably create chained operations that almost look sane:
3 > 1 == True
为真,因为 3 GT; 1
和 1 ==真
都是真实的。但前pression:
is true because 3 > 1
and 1 == True
are both true. But the expression:
3 > 2 == True
是假的,因为 2 ==真
是假的。
1 <子> 布尔
是 INT
历史原因的一个子类; Python的并不总是有一个布尔
类型和超载整数与布尔的意义就像C语言一样。制作布尔
子类保持旧的code工作。
1 bool
is a subclass of int
for historic reasons; Python didn't always have a bool
type and overloaded integers with boolean meaning just like C does. Making bool
a subclass kept older code working.
这篇关于Python的在(__contains__)运算符返回一个布尔值,其值非真非假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!