fixxxer
fixxxer

Reputation: 16144

clarification on comparing objects of different types

The following sentences are a cause of confusion for me(from Guido's Tutorial on python.org):

"Note that comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc."than a tuple, etc."

That means that for :

a=[90]
b=(1)
a<b

the result should be True. But it is not so! Can you help me out here?than a tuple, etc."

Also, what is meant by "The outcome is deterministic but arbitrary"?

Upvotes: 0

Views: 1170

Answers (2)

Daniel Kluev
Daniel Kluev

Reputation: 11315

Please note that you should not rely upon this behavior anymore. Some built-in types cannot be compared with other built-ins, and new data model provides a way to overload comparator functionality.

>>> set([1]) > [1]
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: can only compare to a set

Moreover, it was removed in py3k altogether:

>>> [1,2] > (3,4)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unorderable types: list() > tuple()
>>> [1,2] > "1,2"
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unorderable types: list() > str()

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

(1) is an int. You probably meant (1,), which is a tuple.

Upvotes: 6

Related Questions