"Rob Hunter" <ro*@cs.brown.edu> wrote in message
news:ma**********************************@python.o rg...
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