1

I have searched and no one seems to have this specific question. Why does Python let me compare a list with an integer? For instance,

[] < 10

evaluates to False

and

[] > 10

evaluates to True

Aren't these operations ill-defined and shouldn't Python throw an exception for these operations?

Anirudh
  • 239
  • 3
  • 11
  • 2
    This is basically a design flaw in Python 2. As of Python 3, ill-defined comparisons like this are forbidden: https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons – senshin Jan 09 '15 at 23:47

1 Answers1

3

As of Python 3.x, you are correct this is no longer allowed

>>> [] < 10
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    [] < 10
TypeError: unorderable types: list() < int()

As for why this worked in Python 2.x, read here

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218