変数が任意の数のインスタンス(int
、float
、Fraction
、Decimal
など)であるかどうかを確認しようとします。'decimal.Decimal(1)'が 'numbers.Real'のインスタンスではないのはなぜですか?
私はこの質問とその答えaccrossカム:しかし、私は、このような1j
などの複雑な数字を除外したいHow to properly use python's isinstance() to check if a variable is a number?
。
クラスnumbers.Real
は完璧に見えたが、それはDecimal
数字...矛盾で
from numbers Real
from decimal import Decimal
print(isinstance(Decimal(1), Real))
# False
ためFalse
を返し、それが例えばFraction(1)
で正常に動作します。
documentationには、数字で動作する操作がいくつか記載されていますが、小数点以下の例でエラーなしでテストしました。 10進数のオブジェクトには複素数を含めることはできません。
だから、なぜisinstance(Decimal(1), Real)
がFalse
を返すのですか?
https://docs.python.org/3.6/library/numbers.html#the-numeric-tower –
@TomDalton私はそれを読んで、まだI理解していない。 [Number、Complex、Real、Rational、Integral]の '[isinstance(Decimal(1)、t)'は '[True、False、False、False、False]'を返します。 'Decimal'が' Number'の場合、それはなぜそのサブクラスではないのですか? – Delgan