isNumber? check

Miki Tebeka tebeka at cs.bgu.ac.il
Tue Sep 30 06:57:08 EDT 2003


Hello Rob,
> How do I check if a value is a number in Python?
> 
> One way is (x == type(1)) and (x == type(1.2)) and (x == 
> type(2387482734274)) and ...
> 
> but this seems kludgy.  Any better way?
Same thing, different way:
from types import IntType, LongType, FloatType
def is_num(n):
    return n in (IntType, LongType, FloatType)

HTH.
Miki




More information about the Python-list mailing list