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 s开发者_如何学运维maller 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"?
(1)
is an int
. You probably meant (1,)
, which is a tuple
.
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()
精彩评论