• <small id='yKr26'></small><noframes id='yKr26'>

    <tfoot id='yKr26'></tfoot>

      <legend id='yKr26'><style id='yKr26'><dir id='yKr26'><q id='yKr26'></q></dir></style></legend>

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

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

        如何检查我的 python 对象是否为数字?

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

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

                <tbody id='eZOe3'></tbody>

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

                  本文介绍了如何检查我的 python 对象是否为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Java 中,数字类型都来自 Number,所以我会使用

                  In Java the numeric types all descend from Number so I would use

                  (x instanceof Number).
                  

                  python 等价物是什么?

                  What is the python equivalent?

                  推荐答案

                  测试你的变量是否是 numbers.Number:

                  Test if your variable is an instance of numbers.Number:

                  >>> import numbers
                  >>> import decimal
                  >>> [isinstance(x, numbers.Number) for x in (0, 0.0, 0j, decimal.Decimal(0))]
                  [True, True, True, True]
                  

                  这使用 ABCs 并且适用于所有内置在类似数字的类中,也适用于所有第三方类,如果它们是值得的(注册为 Number ABC 的子类).

                  This uses ABCs and will work for all built-in number-like classes, and also for all third-party classes if they are worth their salt (registered as subclasses of the Number ABC).

                  但是,在许多情况下,您不必担心手动检查类型 - Python 是 duck typed 并且混合一些兼容的类型通常是可行的,但是当某些操作没有意义时(4 - 1")会发出错误消息,因此很少需要手动检查.这只是一个奖金.您可以在完成模块时添加它,以避免在实现细节上纠缠他人.

                  However, in many cases you shouldn't worry about checking types manually - Python is duck typed and mixing somewhat compatible types usually works, yet it will barf an error message when some operation doesn't make sense (4 - "1"), so manually checking this is rarely really needed. It's just a bonus. You can add it when finishing a module to avoid pestering others with implementation details.

                  这适用于从 Python 2.6 开始.在旧版本上,您几乎只能检查一些硬编码类型.

                  This works starting with Python 2.6. On older versions you're pretty much limited to checking for a few hardcoded types.

                  这篇关于如何检查我的 python 对象是否为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何限制 Django 模型中数值字段的最大值? 下一篇:从Python中的字符串中删除所有非数字字符

                  相关文章

                • <small id='gdgry'></small><noframes id='gdgry'>

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

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