isFloat: Without Exception-Handling

Jeff Epler jepler at unpythonic.net
Fri Oct 4 22:31:26 EDT 2002


On Fri, Oct 04, 2002 at 06:05:19PM -0700, Chad Netzer wrote:
> On Friday 04 October 2002 17:59, I wrote:
> 
> > This works better, but does extra work:
> 
> Duh, Chad!  Just change "and" to "or":
> 
> def isFloat(s):
>     try: return float(s) or True
>     except (ValueError, TypeError), e: return False
> 

You mean    
def isFloat(s):
    try: return (float(s), True)[1]
    except (ValueError, TypeError), e: return False

Jeff




More information about the Python-list mailing list