Comparisons of incompatible types
TomF
tomf.sessile at gmail.com
Mon Dec 6 11:59:12 EST 2010
I'm aggravated by this behavior in python:
x = "4"
print x < 7 # prints False
The issue, of course, is comparisons of incompatible types. In most
languages this throws an error (in Perl the types are converted
silently). In Python this comparison fails silently. The
documentation says: "objects of different types *always* compare
unequal, and are ordered consistently but arbitrarily."
I can't imagine why this design decision was made. I've been bitten by
this several times (reading data from a file and not converting the
numbers before comparison). Can I get this to throw an error instead
of failing silently?
Thanks,
-Tom
More information about the Python-list
mailing list