Erik Max Francis <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > > How do I check if a string contains (can be converted to) an int? I > > want to do one thing if I am parsing and integer, and another if not. > > try: > x = int(aPossibleInt) > ... do something with x ... > except ValueError: > ... do something else ...
Correct, but even better is a slight variation: try: x = int(aPossibleInt) except ValueError: ... do something else ... else: ... do something with x ... this way, you avoid accidentally masking an unexpected ValueError in the "do something with x" code. Keeping your try-clauses as small as possible (as well as your except-conditions as specific as possible) is important, to avoid masking bugs and thus making their discovery hader. Alex -- http://mail.python.org/mailman/listinfo/python-list