Comparison operators in Python

Ian Kelly ian.g.kelly at gmail.com
Wed Jun 1 15:16:24 EDT 2011


On Wed, Jun 1, 2011 at 12:50 PM, Anirudh Sivaraman <sk.anirudh at gmail.com> wrote:
> Hi
>
> I am a relative new comer to Python. I see that typing is strongly
> enforced in the sense you can't concatenate or add a string and an
> integer. However comparison between a string and an integer seems to
> be permitted. Is there any rationale behind this ?

It allows things like sorting of heterogeneous lists.  It's generally
viewed as a wart, though, and it was fixed in Python 3:

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 'x' < 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

Cheers,
Ian



More information about the Python-list mailing list