I couldn\'t find another answer when I wanted this, so I thought I would post my own solution for anyone else and also get corrections if I\'ve done something wrong.
def int_float_none(x):
# it may be already int or float
if isinstance(x, (int, float)):
return x
# all int like strings can be converted to float so int tries first
try:
return int(x)
except (TypeError, ValueError):
pass
try:
return float(x)
except (TypeError, ValueError):
return None
Function above for any object passed will return int or float conversion or None.