isFloat: Without Exception-Handling
James T. Dennis
jadestar at idiom.com
Fri Oct 4 20:38:08 EDT 2002
More information about the Python-list mailing list
Fri Oct 4 20:38:08 EDT 2002
- Previous message (by thread): isFloat: Without Exception-Handling
- Next message (by thread): isFloat: Without Exception-Handling
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Mark McEahern <marklists at mceahern.com> wrote: > [Thomas Guettler] >> Is there a way to write the following method without using exceptions? > Please help me understand your aversion to the original code you posted. > IMHO, it has several salient characteristics: > 1. It works. > 2. It works. > 3. It works. > Did I forget to mention: > It works. > Anyway, I'd modify your original function slightly to use True/False > (builtin as of 2.2.1) and to be explicit about which errors we're ignoring: > def isFloat(s): > is_float = True > try: > float(s) > except (ValueError, TypeError), e: > is_float = False > return is_float 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).
- Previous message (by thread): isFloat: Without Exception-Handling
- Next message (by thread): isFloat: Without Exception-Handling
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list