isNumber? check

Rob Hunter rob at cs.brown.edu
Mon Sep 29 22:47:07 CEST 2003


On Monday, September 29, 2003, at 04:41 PM, Gerrit Holl wrote:

> Rob Hunter wrote:
>> 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?
>>>>
>>>>
>>>>
>>>
>>> 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.
>
> So you actually have a string...
>
> Maybe you could do something like this:
>
> if expr.strip("0123456789.e") == "": print "May be a number!"
> if expr.strip("0123456789") == "": print "Surely a number!"
>
> Gerrit.
>

I don't like that solution for this particular project because, as you 
say yourself, if it says yes to the second question, it only *may* be a 
number.  I want to know for sure.  I think the type or instance checks 
as the other posters mentioned are going to be fine for me.

OTOH, thanks for the suggestion!  That's a cool little hack that I'm 
sure will come in handy to me in the future.

Rob






More information about the Python-list mailing list