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

Del*_*gan 11 python numbers decimal isinstance

我尝试检查一个变量是一个数字的任何类型(的一个实例int,float,Fraction,Decimal,等等).

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

但是,我想排除复杂的数字,如1j.

该类numbers.Real看起来完美,但它返回FalseDecimal数字...

from numbers Real
from decimal import Decimal

print(isinstance(Decimal(1), Real))
# False
Run Code Online (Sandbox Code Playgroud)

相反,它可以正常工作Fraction(1).

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

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

Del*_*gan 12

所以,我直接在源代码中找到了答案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).
Run Code Online (Sandbox Code Playgroud)

事实上,加入Decimalfloat将提高TypeError.

在我看来,它违反了最不惊讶的原则,但这并不重要.

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

import numbers
import decimal

Real = (numbers.Real, decimal.Decimal)

print(isinstance(decimal.Decimal(1), Real))
# True
Run Code Online (Sandbox Code Playgroud)