# check whether int conversion will raise an error, two examples. # Python Idioms, http://jaynes.colorado.edu/PythonIdioms.html #LBYL, test for the problematic conditions def test_lbyl (a_str): if not isinstance(a_str, str) or not a_str.isdigit: return None elif len(a_str) > 10: #too many digits for int conversion return None else: return int(a_str) #EAFP, just try it, clean up any mess with handlers def test_eafp(a_str): try: return int(a_str) except (TypeError, ValueError, OverflowError): #int conversion failed return None