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

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

      布尔标识 == True vs 是 True

      时间:2023-09-01
        <tbody id='I5b3h'></tbody>
    2. <small id='I5b3h'></small><noframes id='I5b3h'>

        • <tfoot id='I5b3h'></tfoot>

          <i id='I5b3h'><tr id='I5b3h'><dt id='I5b3h'><q id='I5b3h'><span id='I5b3h'><b id='I5b3h'><form id='I5b3h'><ins id='I5b3h'></ins><ul id='I5b3h'></ul><sub id='I5b3h'></sub></form><legend id='I5b3h'></legend><bdo id='I5b3h'><pre id='I5b3h'><center id='I5b3h'></center></pre></bdo></b><th id='I5b3h'></th></span></q></dt></tr></i><div id='I5b3h'><tfoot id='I5b3h'></tfoot><dl id='I5b3h'><fieldset id='I5b3h'></fieldset></dl></div>
          <legend id='I5b3h'><style id='I5b3h'><dir id='I5b3h'><q id='I5b3h'></q></dir></style></legend>
            <bdo id='I5b3h'></bdo><ul id='I5b3h'></ul>
                本文介绍了布尔标识 == True vs 是 True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                标准约定是使用 if foo is None 而不是 if foo == None 来测试一个值是否具体为 None.

                It is standard convention to use if foo is None rather than if foo == None to test if a value is specifically None.

                如果您想确定一个值是否完全是 True (不仅仅是一个类似 true 的值),是否有任何理由使用 if foo == True 而不是比if foo 为True?这在 CPython(2.x 和 3.x)、Jython、PyPy 等实现之间是否有所不同?

                If you want to determine whether a value is exactly True (not just a true-like value), is there any reason to use if foo == True rather than if foo is True? Does this vary between implementations such as CPython (2.x and 3.x), Jython, PyPy, etc.?

                示例:说 True 用作您想要与值 'bar' 或任何其他类似 true 的值区分开来的单例值:

                Example: say True is used as a singleton value that you want to differentiate from the value 'bar', or any other true-like value:

                if foo is True: # vs foo == True
                    ...
                elif foo == 'bar':
                    ...
                

                是否存在使用 if foo is True 会产生与 if foo == True 不同的结果的情况?

                Is there a case where using if foo is True would yield different results from if foo == True?

                注意:我知道 Python 布尔值 - if x:, vs if x == True, vs if x is True.但是,它只处理 if fooif foo == Trueif foo is True 通常是否应该用于确定 foo 有一个类似于 true 的值.

                NOTE: I am aware of Python booleans - if x:, vs if x == True, vs if x is True. However, it only addresses whether if foo, if foo == True, or if foo is True should generally be used to determine whether foo has a true-like value.

                更新:根据 PEP 285 § 规范:

                UPDATE: According to PEP 285 § Specification:

                值 False 和 True 将是单例,例如 None.

                The values False and True will be singletons, like None.

                推荐答案

                如果您想确定一个值是否完全为 True(而不仅仅是类似 true 的值),是否有任何理由使用 if foo == True 而不是 if foo is True?

                If you want to determine whether a value is exactly True (not just a true-like value), is there any reason to use if foo == True rather than if foo is True?

                如果您想确保 foo 确实是一个布尔值且值为 True,请使用 is 运算符.

                If you want to make sure that foo really is a boolean and of value True, use the is operator.

                否则,如果 foo 的类型实现了它自己的 __eq__() 在与 True 比较时返回一个真值,你可能会导致意想不到的结果.

                Otherwise, if the type of foo implements its own __eq__() that returns a true-ish value when comparing to True, you might end up with an unexpected result.

                根据经验,您应该始终将 is 与内置常量 TrueFalseNone<一起使用/代码>.

                As a rule of thumb, you should always use is with the built-in constants True, False and None.

                这在 CPython(2.x 和 3.x)、Jython、PyPy 等实现之间是否有所不同?

                Does this vary between implementations such as CPython (2.x and 3.x), Jython, PyPy, etc.?

                理论上,is 会比 == 更快,因为后者必须遵守类型的自定义 __eq__ 实现,而 is 可以直接比较对象的身份(例如,内存地址).

                In theory, is will be faster than == since the latter must honor types' custom __eq__ implementations, while is can directly compare object identities (e.g., memory addresses).

                我不知道各种 Python 实现的源代码,但我认为它们中的大多数可以通过使用一些内部标志来优化它,以确保魔法方法的存在,所以我怀疑你不会注意到实践中的速度差异.

                I don't know the source code of the various Python implementations by heart, but I assume that most of them can optimize that by using some internal flags for the existence of magic methods, so I suspect that you won't notice the speed difference in practice.

                这篇关于布尔标识 == True vs 是 True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

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

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