i will be comparing two values like this:\
value1>value2
i know that value2 is always an integer, but sometimes value1 is None or a string, how do force the comparison ONLY if value1 is numerical?
value1 is a decimal
if value1:
Decimal(value1) > value2
int cast won't work pleasantly on a decimal. And the isdigit() only works on strings. You can check for the existance of isdigit with hasattr and change the int to Decimal to make sure you can compare both ints and Decimals.Decimal) than you can do with a simple if value1 and a Decimal(value1) > value2try:
value1 > value2
except TypeError:
pass
if isinstance( value2, int ):
value1 > value2
This latter is unpythonic, because this type of comparison is unpythonic. You should filter your data first.
ints. You should try and think a bit about what the code is doing before asking such simple questions.try:
int(value1) > value2
except (TypeError, ValueError):
pass