Test for number?

Peter Maas peter.maas at somewhere.com
Mon Sep 4 16:02:54 EDT 2006


Tim Williams wrote:
> On 04/09/06, Dr. Pastor <elpX at adsihqx.com> wrote:
>> In the following code I would like to ascertain
>> that x has/is a number. What the simplest TEST should be?

def isnumber(value):
    try:
        value/1
        return true
    except TypeError:
        return false

I think this works for all builtin types. Of course one could
overload '/' for a non-numeric class. The best solution would
be to introduce a super class base_number for int, long, float
and decimal, similar to base_string for str and unicode.

This would probably be not too hard to implement and surely not
break old code :)

Peter Maas, Aachen



More information about the Python-list mailing list