[Zope] Python Function to Test for Integer Type
Terry Hancock
hancock at anansispaceworks.com
Tue Jun 15 19:29:59 EDT 2004
On Tuesday 15 June 2004 01:00 pm, Asad Habib wrote:
> Hello. Does anyone know of a python function that returns a value based on
> whether or not its argument is an integer? Any help would be greatly
> appreciated. Thanks.
Well, the usual way to check in Python is an idiom like:
if type(spam)==type(1):
print "Yep, it's an integer."
else:
print "Whoops. Not an integer."
so it's not a function, but an expression.
Unfortunately, Zope doesn't allow you to use "type()" in a Python
script for mysterious security reasons. So, it's either go to
an external method, allow that import, or find out an alternate
way to do it.
*Converting* to an integer is done with int(), though. And you
could always ask:
if int(spam)==spam:
print "Yep, it's an integer."
Technically this would accept "1.0000" as well as "1", but it's
unclear to me why you would care about the actual storage format
versus the meaning. It won't accept '1', though, which might
matter if spam comes from a web form.
Cheers,
Terry
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
More information about the Zope
mailing list