[Python-talk] Does a string contain an integer

Lloyd Kvam python at venix.com
Thu Apr 3 18:46:42 EDT 2008


On Thu, 2008-04-03 at 12:49 -0700, Dick Moores wrote:
> On Thu, Apr 3, 2008 at 7:49 AM, Lloyd Kvam <lkvam at venix.com> wrote:
> >
> > On Wed, 2008-04-02 at 23:05 -0700, Dick Moores wrote:
> >
> > > On Wed, Apr 2, 2008 at 9:06 AM, Lloyd Kvam <lkvam at venix.com> wrote:
> > > I'll keep this function around:
> > >
> > > def str_is_int(astr):
> > >     try:
> > >         int(astr)
> > >         return True
> > >     except:
> > >         return False
> > >
> > Still if your code looks like
> >
> > if str_is_int(astr):
> >    aint = int(astr)
> > else:
> >    handle_not_an_int(astr)
> >    ???
> >
> > wouldn't you be just as well off coding the try / except ValueError
> > directly?
> 
> I just wanted the function in my toolbox. I don't have any code right
> now to apply it to.
> 
> > try:
> >    aint = int(astr)
> > except ValueError:
> >    handle_not_an_int(astr)
> 
> Could you expand on that a bit? What does the 4th line mean?

Well, what do the coding choices look like?

---- case 1 ----
# we let the ValueError happen
aint = int(astr)	# raises ValueError if it can't be converted
print "You've won %d Dollars" % aint

somewhere earlier in your code you have a try / except that will deal
with the error.  

Or maybe you don't.  Perhaps there is a GUI that provides a list of
valid strings, but somehow they managed to feed in an invalid string.
You've decided to simply let the program die on the unhandled exception.
------------------

---- case 2 ----
if str_is_int(astr):
	aint = int(astr)
	# True case handler
	# do something with the integer value
else:
	# False case handler
	# presumably you provide an error message

To my mind, you only use the str_is_int function if you are concerned
with doing something when the conversion would fail.
----------------------------------
But I don't think that function provides much benefit.  You could just
as easily code case 2 to look like:

try:	#keep try block small by using else
	aint = int(astr)
except ValueError:
	# False case handler
	# presumably you provide an error message
else:	
	# True case handler - astr was converted to an int
	# do something with the integer value

If you choose to use a default value for the integer when the string is
defective, you could code

try:
	aint = int(astr)
except ValueError:
	aint = 0	# or some other default
print "You've won %d Dollars" % aint



Ideally, your error handling was put in place earlier and most of the
time you simply code
	aint = int(astr)

If the error handling needs to be addressed here, I still see no benefit
to putting the try/except logic into a function that returns a boolean.
The Python try / except / else control statements seem to be the right
tools for the job.

> 
> Thanks,
> 
> Dick Moores
> _______________________________________________
> Python-talk mailing list
> Python-talk at dlslug.org
> http://dlslug.org/mailman/listinfo/python-talk
-- 
Lloyd Kvam
Venix Corp
DLSLUG/GNHLUG library
http://www.librarything.com/catalog/dlslug
http://www.librarything.com/profile/dlslug
http://www.librarything.com/rsshtml/recent/dlslug



More information about the Python-talk mailing list