<tfoot id='CYAZY'></tfoot><legend id='CYAZY'><style id='CYAZY'><dir id='CYAZY'><q id='CYAZY'></q></dir></style></legend>

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

        • <bdo id='CYAZY'></bdo><ul id='CYAZY'></ul>

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

        使用 isinstance 比较 boolean 和 int

        时间:2023-09-11
        • <i id='pCEOW'><tr id='pCEOW'><dt id='pCEOW'><q id='pCEOW'><span id='pCEOW'><b id='pCEOW'><form id='pCEOW'><ins id='pCEOW'></ins><ul id='pCEOW'></ul><sub id='pCEOW'></sub></form><legend id='pCEOW'></legend><bdo id='pCEOW'><pre id='pCEOW'><center id='pCEOW'></center></pre></bdo></b><th id='pCEOW'></th></span></q></dt></tr></i><div id='pCEOW'><tfoot id='pCEOW'></tfoot><dl id='pCEOW'><fieldset id='pCEOW'></fieldset></dl></div>
            <bdo id='pCEOW'></bdo><ul id='pCEOW'></ul>
              <legend id='pCEOW'><style id='pCEOW'><dir id='pCEOW'><q id='pCEOW'></q></dir></style></legend>
              <tfoot id='pCEOW'></tfoot>
                  <tbody id='pCEOW'></tbody>

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

                2. 本文介绍了使用 isinstance 比较 boolean 和 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有人能给我解释一下为什么 isinstance() 在以下情况下返回 True 吗?在编写代码时,我期望 False.

                  Can someone give me an explanation why isinstance() returns True in the following case? I expected False, when writing the code.

                  print isinstance(True, (float, int))
                  True
                  

                  我的猜测是它的 Python 的内部子类化,零和一 - 无论是浮点数还是整数 - 都在用作布尔值时进行评估,但不知道确切的原因.

                  My guess would be that its Python's internal subclassing, as zero and one - whether float or int - both evaluate when used as boolean, but don't know the exact reason.

                  解决这种情况的最pythonic方法是什么?我可以使用 type() 但在大多数情况下,这被认为不那么 Pythonic.

                  What would be the most pythonic way to solve such a situation? I could use type() but in most cases this is considered less pythonic.

                  推荐答案

                  由于历史原因,boolint 的子类,所以 Trueint 的一个实例.(最初,Python 没有 bool 类型,返回真值的东西返回 1 或 0.当他们添加 bool 时,True 和 False 必须尽可能地替换 1 和 0 以实现向后兼容性,因此是子类化.)

                  For historic reasons, bool is a subclass of int, so True is an instance of int. (Originally, Python had no bool type, and things that returned truth values returned 1 or 0. When they added bool, True and False had to be drop-in replacements for 1 and 0 as much as possible for backward compatibility, hence the subclassing.)

                  解决"这个问题的正确方法取决于您认为问题是什么.

                  The correct way to "solve" this depends on exactly what you consider the problem to be.

                  • 如果你想让 True 不再是 int,那太糟糕了.这不会发生.
                  • 如果您想检测布尔值并以不同于其他整数的方式处理它们,您可以这样做:

                  • If you want True to stop being an int, well, too bad. That's not going to happen.
                  • If you want to detect booleans and handle them differently from other ints, you can do that:

                  if isinstance(whatever, bool):
                      # special handling
                  elif isinstance(whatever, (float, int)):
                      # other handling
                  

                3. 如果你想检测特定类正好是floatint的对象,拒绝子类,你可以这样做:

                4. If you want to detect objects whose specific class is exactly float or int, rejecting subclasses, you can do that:

                  if type(whatever) in (float, int):
                      # Do stuff.
                  

                5. 如果您想检测所有浮点数和整数,您已经在这样做了.
                6. 这篇关于使用 isinstance 比较 boolean 和 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:有没有办法获得可以在 Python 中使用的最大整数 下一篇:pandas :减去两个日期列,结果是一个整数

                  相关文章

                  最新文章

                7. <tfoot id='DXt4l'></tfoot>
                    <bdo id='DXt4l'></bdo><ul id='DXt4l'></ul>

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

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