[英]Checking if the number is a decimal.Decimal type in python
Variable 'a' could be of type - int/float/decimal.Decimal (but not a string)变量“a”的类型可以是 - int/float/decimal.Decimal(但不是字符串)
I want to check if its a decimal.Decimal type.我想检查它是否是 decimal.Decimal 类型。
Following works:以下作品:
import decimal
a = decimal.Decimal(4)
if type(a) is decimal.Decimal:
print('yes decimal')
else:
print('not decimal')
But, is there a righter way of doing the same?但是,有没有更正确的方法来做同样的事情? tnx.谢谢。
Use isinstance
which outputs True
or False
.使用输出True
或False
的isinstance
。
result = isinstance(a, decimal.Decimal)
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.