isNumber? check

Rob Hunter rob at cs.brown.edu
Mon Sep 29 16:27:33 EDT 2003


On Monday, September 29, 2003, at 10:20 AM, Gerrit Holl wrote:

> Rob Hunter wrote:
>> 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?
>
> Why do you want to do so?


I am writing an interpreter, and my parser has to decide if an 
expression is a number or not.



> Maybe, it is better in your
> case to just run the piece of code using the number, and
> if it fails, it fails.
> However, if you must, you need to
> do type(x) is type(1) and ... etc., or isinstance(x, int)
> and isinstance(x, float), etc.
>
> Gerrit.






More information about the Python-list mailing list