Reputation: 147
Variable 'a' could be of type - int/float/decimal.Decimal (but not a string)
I want to check if its a decimal.Decimal type.
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.
Upvotes: 5
Views: 5769
Reputation: 304
Use isinstance
which outputs True
or False
.
result = isinstance(a, decimal.Decimal)
print(result)
Upvotes: 6