Comparison oddities

Chuck Esterbrook echuck at mindspring.com
Fri Feb 2 13:53:27 EST 2001


It appears that tuples and strings are "greater than" lists.

 >python
ActivePython 2.0, build 202 (ActiveState Tool Corp.)
based on Python 2.0 (#8, Oct 19 2000, 11:30:05) [MSC 32 bit (Intel)] on win32
 >>> a = (1, 1)
 >>> b = [1, 1]
 >>> a < b
0
 >>> a > b
1
 >>> a==b
0
 >>> a = '1'
 >>> a < b
0
 >>> a > b
1


Does this seem a little odd to anyone?

Since tuples and lists are both "sequences of anything", you would hope 
that they would compare in "the natural fashion".

Since comparing a string and a list doesn't make sense, would an exception 
be more appropriate?

And yes, I know the manual says "Otherwise, objects of different types 
*always* compare unequal, and are ordered consistently but arbitrarily. ... 
In the future, the comparison rules for objects of different types are 
likely to change."

My question is more of a design question.


-Chuck





More information about the Python-list mailing list