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

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

      1. <tfoot id='tFoKw'></tfoot>
        <legend id='tFoKw'><style id='tFoKw'><dir id='tFoKw'><q id='tFoKw'></q></dir></style></legend>
          <bdo id='tFoKw'></bdo><ul id='tFoKw'></ul>

        为什么'decimal.Decimal(1)'不是'numbers.Real'的一个实例

        Why is not #39;decimal.Decimal(1)#39; an instance of #39;numbers.Real#39;?(为什么decimal.Decimal(1)不是numbers.Real的一个实例?)

            <tbody id='3VC3v'></tbody>

                <tfoot id='3VC3v'></tfoot>
              • <small id='3VC3v'></small><noframes id='3VC3v'>

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

                <legend id='3VC3v'><style id='3VC3v'><dir id='3VC3v'><q id='3VC3v'></q></dir></style></legend>
                  <bdo id='3VC3v'></bdo><ul id='3VC3v'></ul>
                • 本文介绍了为什么'decimal.Decimal(1)'不是'numbers.Real'的一个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试检查一个变量是否是任何类型(intfloatFraction十进制等).

                  I try to check if a variable is an instance of a number of any type (int, float, Fraction, Decimal, etc.).

                  我遇到了这个问题及其答案:如何正确使用 python 的 isinstance() 检查变量是否为数字?

                  I cam accross this question and its answer: How to properly use python's isinstance() to check if a variable is a number?

                  但是,我想排除复数,例如 1j.

                  However, I would like to exclude complex numbers such as 1j.

                  numbers.Real 看起来很完美,但它为 <返回 Falsecode>十进制 数字...

                  The class numbers.Real looked perfect but it returns False for Decimal numbers...

                  from numbers Real
                  from decimal import Decimal
                  
                  print(isinstance(Decimal(1), Real))
                  # False
                  

                  相反,它适用于 Fraction(1) 例如.

                  In contradiction, it works fine with Fraction(1) for example.

                  文档描述了一些应该与数字,我在十进制实例上测试它们没有任何错误.此外,十进制对象不能包含复数.

                  The documentation describes some operations which should work with the number, I tested them without any error on a decimal instance. Decimal objects cannot contains complex numbers moreover.

                  那么,为什么 isinstance(Decimal(1), Real) 会返回 False?

                  So, why isinstance(Decimal(1), Real) would return False?

                  推荐答案

                  所以,我直接在cpython/numbers.py:

                  So, I found the answer directly in the source code of cpython/numbers.py:

                  ## Notes on Decimal
                  ## ----------------
                  ## Decimal has all of the methods specified by the Real abc, but it should
                  ## not be registered as a Real because decimals do not interoperate with
                  ## binary floats (i.e.  Decimal('3.14') + 2.71828 is undefined).  But,
                  ## abstract reals are expected to interoperate (i.e. R1 + R2 should be
                  ## expected to work if R1 and R2 are both Reals).
                  

                  确实,将 Decimal 添加到 float 会引发 TypeError.

                  Indeed, adding Decimal to float would raise a TypeError.

                  在我看来,它违反了最小惊讶的原则,但没关系.

                  In my point of view, it violates the principle of least astonishment, but it does not matter much.

                  作为一种解决方法,我使用:

                  As a workaround, I use:

                  import numbers
                  import decimal
                  
                  Real = (numbers.Real, decimal.Decimal)
                  
                  print(isinstance(decimal.Decimal(1), Real))
                  # True
                  

                  这篇关于为什么'decimal.Decimal(1)'不是'numbers.Real'的一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                      <tbody id='rH174'></tbody>
                    <tfoot id='rH174'></tfoot>

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

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