isFloat: Without Exception-Handling
marklists at mceahern.com
Fri Sep 20 15:50:15 CEST 2002
> 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:
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:
is_float = True
except (ValueError, TypeError), e:
is_float = False
assert not isFloat(4+0j)
assert not isFloat("banana")
assert not isFloat(isFloat)
assert not isFloat(__import__)
More information about the Python-list