isFloat: Without Exception-Handling

Robin Becker robin at jessikat.fsnet.co.uk
Thu Jun 12 03:54:05 EDT 2003


In article <v_UFa.2709$Mi3.161728669 at newssvr21.news.prodigy.com>, Byron
Morgan <lazypointer at yahoo.com> writes
>This works:
>
>import operator
>def isFloat(f):
>    if not operator.isNumberType(f):
>        return 0
>    if f % 1:
>        return 1
>    else:
>        return 0
>
>>>> isFloat(3.1)
>1
>>>> isFloat('a')
>0
>>>> isFloat(12345)
>0
..... with 2.2.3 I see 
>>> isFloat('a')
0
>>> isFloat(3.0)
0
>>> isFloat(3.1)
1
>>>

so isFloat isn't testing the type, but the value. I suggest

>>> from types import FloatType
>>> from operator import isNumberType
>>> def isFloat(f):
...     return isNumberType(f) and (type(f) is FloatType) or 0
... 
-- 
Robin Becker




More information about the Python-list mailing list