Pythonic way to determine if a string is a number
python at bdurham.com
python at bdurham.com
Sun Feb 15 15:40:47 EST 2009
Tim,
> Just for the info, Malcolm, you don't actually need to assign the result of float (input) to anything if you don't need to use it. All you're looking for is the exception. Let the intepreter convert it and then throw it away.
Yes!
> Also, as an alternative style which can be more appropriate depending on the structure and intention of what you're doing, you can use the else: clause of the try-except block.
>
> try:
> float (input)
> except ValueError:
> return False
> else:
> return True
I follow the semantics, but I don't know why I would prefer the try/else
technique over the simpler:
try:
float( input )
return True
except ValueError:
return False
Thank you for your feedback,
Malcolm
More information about the Python-list
mailing list