[Tutor] casting string to integer in a list of lists

wesley chun wescpy at gmail.com
Fri Jan 9 19:12:06 CET 2009


>> A tuple of exceptions works, just like what we did above, and more,
>> i.e., (IndexError, ValueError, TypeError, KeyError...
>>
> Thank you, thank you, thank you!  I'm sure it's been staring me in the face,
> but I never realized I could use a tuple of exception types - that's why I
> said it was a pain in the butt, trapping the exception and doing
> "isinstance" checks against it.  How did I miss this?

wow, you were really going out of your way. i believe the docs have
some examples but cannot confirm this.

> Learn something new every day, no?

even i do.

oh, here's something else for the OP...

i forgot to mention there is a useful string method called isdigit()
that you can use on the string in question. it returns True if all the
chars in the string are digits, meaning that it's likely safe to call
int() on it.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list