isFloat: Without Exception-Handling

Chad Netzer cnetzer at mail.arc.nasa.gov
Sat Oct 5 02:59:46 CEST 2002


On Friday 04 October 2002 17:38, James T. Dennis wrote:
>
>  What's wrong with:
>
>  def isFloat(s):
> 	try: return float(s) and True
> 	except (ValueError, TypeError), e: return False

>>> isFloat( 1 )
1
>>> isFloat( 0 )
0.0
>>> isFloat( 0.0 )
0.0

This works better, but does extra work:

def isFloat(s):
    try: return (float(s) == float(s))
    except (ValueError, TypeError), e: return False

-- 

Chad Netzer
cnetzer at mail.arc.nasa.gov




More information about the Python-list mailing list