Python Function to Test for Integer Type
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. - Asad
On Tue, Jun 15, 2004 at 02:00:35PM -0400, 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.
pretty trivial to roll your own... I'm assuming you also want it to return true for longs: import types def is_int(arg): return type(arg) in (types.IntType, types.LongType) In zope's Script (Python), you can't do that because type() is not allowed; but you can make do with the same_type function: return same_type(arg, 1) or same_type(arg, 1L) -- Paul Winkler http://www.slinkp.com
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
Or use the zope function same_type: if same_type(1,somevar): print '"it's an int' 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.
*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
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
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
On Tuesday 15 June 2004 04:29 pm, 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.
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.
For this to work properly, you'd want to catch exceptions on the int call (which would happen for '1.00' among other less sensible values). If data were coming from the web and I wanted to convert sensible int-like values to ints, and accept things like 1.00 and '1.00', I might do: try: if int(float(spam)) == float(spam): spam == int(float(spam)) return "If it wasn't an integer before, it is now." else: return "That's not an integer, it's a float!" except ValueError, TypeError: return "That's nothing like an integer!" But that is certainly overkill if you really want to just check for the python type integer. Good luck, Alec Mitchell
On Tue, Jun 15, 2004 at 06:29:59PM -0500, Terry Hancock wrote:
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.
Sure it will. $ python Python 2.3.3 (#1, Feb 2 2004, 03:53:32) [GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r3, propolice)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
int(1) == int(1.0) == int('1') True
-- Paul Winkler http://www.slinkp.com
On Wednesday 16 June 2004 01:10 am, Paul Winkler wrote:
Python 2.3.3 (#1, Feb 2 2004, 03:53:32) [GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r3, propolice)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
int(1) == int(1.0) == int('1') True
That's not what I wrote -- and it would always be true. *This* doesn't work: Python 2.3.4 (#2, May 29 2004, 03:31:27) [GCC 3.3.3 (Debian 20040417)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
int(1)=='1' False
which is equivalent to the test I had above. But this does:
int(1)==1.0 True
Strings equate differently from numbers. The test will fail though if the floating point value isn't *exactly* an integer.
int(1)==1.000001 False
Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
The test will fail though if the floating point value isn't *exactly* an integer.
int(1)==1.000001
False
Interesting::
1==1.00000000000000001 True 1==1.000000000000001 False
Cheers, Terry
-- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
.
-- Peter Bengtsson, http://www.peterbe.com
On Thursday 17 June 2004 05:31 am, Peter Bengtsson wrote:
The test will fail though if the floating point value isn't *exactly* an integer.
int(1)==1.000001
False
Interesting::
1==1.00000000000000001 True 1==1.000000000000001 False
Yeah, means you exceeded the floating point precision of the storage format. :-) See:
f = 1.000000000000001 f 1.0000000000000011 f = 1.00000000000000001 f 1.0
That's what "exactly" means on a computer. :-D Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
participants (7)
-
Alec Mitchell -
Asad Habib -
John E. Barham -
Paul Winkler -
Peter Bengtsson -
Phil Harris -
Terry Hancock