Op 28-08-12 10:06, Richard D. Moores schreef:
On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang <jerry.scofi...@gmail.com> wrote:
2012/8/28 Richard D. Moores <rdmoo...@gmail.com>
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?
a = 4
isinstance(a, int)
True
isinstance(3., int)
False
What if I wanted 3., 1234., etc. to be considered ints, as they are by
_validate_int() ?
>>> isinstance(3., (int, float))
True
Because 3. is a float, not int.
Timo
Dick
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor