On Mon, 27 Aug 2012, Richard D. Moores wrote:

On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett <ja...@pearachute.com> wrote:

something like:

def _validate_int(obj):
    """Raise an exception if obj is not an integer."""
    m = int(obj + 0)  # May raise TypeError.
    if obj != m:
        raise ValueError('expected an integer but got %r' % obj)


is a really awkward way to test if something's an integer, and checking
types in general is usually a sign of larger flaws in laying out useful
code.

What the best way to test if something's an integer?

try:
    whatever_you_want(supposed_integer)
except ValueError:
    print("Oops, that wasn't an integer! Please try again")

That's usually the best way...

HTH,
Wayne
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to