isNumber? check
Gerrit Holl
gerrit at nl.linux.org
Mon Sep 29 16:41:43 EDT 2003
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?
> >>
> >>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?
>
>
> 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.
--
249. If any one hire an ox, and God strike it that it die, the man who
hired it shall swear by God and be considered guiltless.
-- 1780 BC, Hammurabi, Code of Law
--
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