我尝试检查一个变量是否是任何类型(int、float、Fraction、Decimal等)的多个实例。
我遇到了这个问题及其答案:How to proper use python's isinstance() to check if a variable is a number?
但是,我想排除复数,例如1j.
这门课numbers.Real看起来很完美,但它返回False了Decimal数字......
from numbers Real
from decimal import Decimal
print(isinstance(Decimal(1), Real))
# False
与此相反,它适用Fraction(1)于例如。
文档描述了一些应该使用数字的操作,我在十进制实例上测试它们没有任何错误。此外,十进制对象不能包含复数。
那么,为什么isinstance(Decimal(1), Real)会返回False呢?