isNumber? check

Terry Reedy tjreedy at udel.edu
Mon Sep 29 11:28:56 EDT 2003


"Rob Hunter" <rob at cs.brown.edu> wrote in message
news:mailman.1064844761.11275.python-list at python.org...
> 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?

type(x) in (int, long, float, complex) # or,
isinstance(x, (int, long, float, complex)) # now preferred, I think

But how about x=x-0?
This also accepts a user-defined number-class instance.

This option gets to the point that 'number' is not exactly defined in
either Python or mathematics.  So checking for 'numberness' may or may
not be what you need .

Terry J. Reedy






More information about the Python-list mailing list