Python type checking?

Jeff jam at quark.emich.edu
Tue Dec 7 11:04:40 EST 1999


On Tue, Dec 07, 1999 at 03:43:06PM +0000, Arinte wrote:
> class PossArg:
>    def __init__(self,argname, argvalue):
>       self.argname = argname
>       self.argvalue = argvalue
> 
>    def getValue(self):
>       return self.argvalue
>    def getName(self):
>       return self.argname
> 
>    def setValue(self, value):
>       self.argvalue = value
> 
> On the set setValue(), they set it to an integer value I want to make
> it a long else if it is a string leave it as a string.  How can this be
> done?  I figure I can do a if (type(argvalue)=="int"), but how would I
> handle a cast in python.
> 
> TIA
> 

[~] [11:01am] [jam at quark-pts/5] % python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
(egcs-1.1.1  on linux2)
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> a = "1"
>>> print int(a)
1
>>> a = 1
>>> print int(a)
1
>>> a = "1"
>>> print long(a)
1L
>>> a = 1
>>> print long(a)
1L

this works just fine, though you still might want to wrap the code into a
try/except in case the string *isn't* a value that the 'int' or 'long'
functions can handle.

hope that helps.

regards,
J
-- 
|| visit gfd <http://quark.emich.edu/>
|| psa member #293 <http://www.python.org/> 
|| New Image Systems & Services, Inc. <http://www.newimage.com/>




More information about the Python-list mailing list