isFloat: Without Exception-Handling

Mark McEahern marklists at mceahern.com
Fri Oct 4 20:48:33 EDT 2002


[James T. Dennis]
>  What's wrong with:
>
>  def isFloat(s):
> 	try: return float(s) and True
> 	except (ValueError, TypeError), e: return False
>
>  It seems more concise and clear to skip the temp variable and
>  assignments.
>
>  (The 'and True' part of the first return expression implicitly coerces
>  the return result to be a boolean, while the phrase: "and True" seems
>  clear enough in English, a little stilted, but still good enough
> English).

That you needed to explain that is kind of funny in light of your claim that
this is somehow clearer.  <wink>  I agree with skipping the temp variable in
favor of just doing an immediate return True/False in the appropriate
branch, but what's with the OCD (obsessive compulsive disorder) re:
collapsing lines?  This ain't Perl golf, you know.

Cheers,

// mark





More information about the Python-list mailing list