[Zope] Python Function to Test for Integer Type
John E. Barham
jbarham at jbarham.com
Tue Jun 15 19:33:04 EDT 2004
Terry Hancock wrote:
> 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.
The alternate function to type() that Zope provides is same_type():
if same_type(value, 1):
print "value is integer"
elif same_type (value, "abc"):
print "value is string"
elif same_type(value, 1.0):
print "value is float"
You can try casting the value to an int and catch the exception if it fails:
try:
int_val = int(value)
except ValueError:
print "value is not integer"
John
More information about the Zope
mailing list