comparison on list yields surprising result

Diez B. Roggisch deets at nospam.web.de
Fri Aug 28 15:19:43 EDT 2009


Dr. Phillip M. Feldman schrieb:
> In [21]: x
> Out[21]: [1, 2, 3, 5]
> 
> In [22]: x>6
> Out[22]: True
> 
> Is this a bug?

In python2.x, it's the defined behavior - all types are somhow 
comparable. The comparison is stable (all lists compare larger to all 
ints), but of course this by no means well-defined.

This debatable design-decision has been remedied in Python3:

Python 3.1 (r31:73578, Jun 27 2009, 21:49:46)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> list(range(10)) < 10
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unorderable types: list() < int()
 >>>





Diez



More information about the Python-list mailing list