<tfoot id='UACBn'></tfoot>

  1. <small id='UACBn'></small><noframes id='UACBn'>

  2. <legend id='UACBn'><style id='UACBn'><dir id='UACBn'><q id='UACBn'></q></dir></style></legend>
      <i id='UACBn'><tr id='UACBn'><dt id='UACBn'><q id='UACBn'><span id='UACBn'><b id='UACBn'><form id='UACBn'><ins id='UACBn'></ins><ul id='UACBn'></ul><sub id='UACBn'></sub></form><legend id='UACBn'></legend><bdo id='UACBn'><pre id='UACBn'><center id='UACBn'></center></pre></bdo></b><th id='UACBn'></th></span></q></dt></tr></i><div id='UACBn'><tfoot id='UACBn'></tfoot><dl id='UACBn'><fieldset id='UACBn'></fieldset></dl></div>
      • <bdo id='UACBn'></bdo><ul id='UACBn'></ul>

      Python 的 in (__contains__) 运算符返回一个布尔值,其值既不是 True 也不是 False

      Python#39;s in (__contains__) operator returns a bool whose value is neither True nor False(Python 的 in (__contains__) 运算符返回一个布尔值,其值既不是 True 也不是 False)
      • <bdo id='nX1rh'></bdo><ul id='nX1rh'></ul>
          <legend id='nX1rh'><style id='nX1rh'><dir id='nX1rh'><q id='nX1rh'></q></dir></style></legend>

              <tbody id='nX1rh'></tbody>
            <i id='nX1rh'><tr id='nX1rh'><dt id='nX1rh'><q id='nX1rh'><span id='nX1rh'><b id='nX1rh'><form id='nX1rh'><ins id='nX1rh'></ins><ul id='nX1rh'></ul><sub id='nX1rh'></sub></form><legend id='nX1rh'></legend><bdo id='nX1rh'><pre id='nX1rh'><center id='nX1rh'></center></pre></bdo></b><th id='nX1rh'></th></span></q></dt></tr></i><div id='nX1rh'><tfoot id='nX1rh'></tfoot><dl id='nX1rh'><fieldset id='nX1rh'></fieldset></dl></div>
            1. <tfoot id='nX1rh'></tfoot>
            2. <small id='nX1rh'></small><noframes id='nX1rh'>

                本文介绍了Python 的 in (__contains__) 运算符返回一个布尔值,其值既不是 True 也不是 False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                正如所料,1 不包含在空元组中

                As expected, 1 is not contained by the empty tuple

                >>> 1 in ()
                False
                

                但是返回的False值不等于False

                >>> 1 in () == False
                False
                

                换个角度看,in 运算符返回一个 bool,它既不是 True 也不是 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)
                

                但是,如果原始表达式被括号括起来,则正常行为会恢复

                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 in () == False not 表示 (1 in ()) == False.

                You are running into comparison operator chaining; 1 in () == False does not mean (1 in ()) == False.

                相反,比较是链式的,表达式的真正含义是:

                Rather, comparisons are chained and the expression really means:

                (1 in ()) and (() == False)
                

                因为 (1 in ()) 已经为假,链式表达式的后半部分被完全忽略(因为 False and something_else 返回 False 无论 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 <y <= z 等价于 x <y 和 y <= z,除了 y 只计算一次(但在这两种情况下,当 x 时根本不计算 z; y 被发现为假).

                Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

                作为记录,<, >, ==, >=, <=!=isis notinnot in 都是比较运算符(不推荐使用的 <> 也是如此).

                For the record, <, >, ==, >=, <=, !=, is, is not, in and not in are all comparison operators (as is the deprecated <>).

                一般来说,不要与布尔值进行比较;只需测试表达式本身.如果您 需要针对布尔文字进行测试,至少使用括号和 is 运算符、TrueFalse是单例,就像 None:

                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 bool 类型是 int1 的子类.因此,False == 0 为真,True == 1 也是如此.因此,您可以想象创建看起来很正常的链式操作:

                This gets more confusing still when integers are involved. The Python bool type is a subclass of int1. 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 >11 == True 都是真的.但表达式:

                is true because 3 > 1 and 1 == True are both true. But the expression:

                3 > 2 == True
                

                是假的,因为 2 == True 是假的.

                is false, because 2 == True is false.

                1 bool 由于历史原因是 int 的子类;Python 并不总是像 C 那样具有 bool 类型和具有布尔含义的重载整数.将 bool 设为子类可以让旧代码继续工作.

                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 的 in (__contains__) 运算符返回一个布尔值,其值既不是 True 也不是 False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <tbody id='uzase'></tbody>
                • <bdo id='uzase'></bdo><ul id='uzase'></ul>
                • <i id='uzase'><tr id='uzase'><dt id='uzase'><q id='uzase'><span id='uzase'><b id='uzase'><form id='uzase'><ins id='uzase'></ins><ul id='uzase'></ul><sub id='uzase'></sub></form><legend id='uzase'></legend><bdo id='uzase'><pre id='uzase'><center id='uzase'></center></pre></bdo></b><th id='uzase'></th></span></q></dt></tr></i><div id='uzase'><tfoot id='uzase'></tfoot><dl id='uzase'><fieldset id='uzase'></fieldset></dl></div>

                          <small id='uzase'></small><noframes id='uzase'>

                        1. <tfoot id='uzase'></tfoot>
                          <legend id='uzase'><style id='uzase'><dir id='uzase'><q id='uzase'></q></dir></style></legend>