isNumber? check

Gerrit Holl gerrit at nl.linux.org
Mon Sep 29 16:20:27 CEST 2003


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? 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.

--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list