>>> [1, 2] > 'foo' # 'list' < 'str'
False
>>> (1, 2) > 'foo' # 'tuple' > 'str'
True
>>> class Foo(object): pass
>>> class Bar(object): pass
>>> Bar() < Foo()
True
2.当数值对象与非数值对象进行比较时,数值对象<非数值对象
>>> 5 < 'foo'
True
>>> 5 < (1, 2)
True
>>> 5 < {}
True
>>> 5 < [1, 2]
True
3.old-style型的对象>new-style型的对象
>>> class Foo: pass # old-style
>>> class Bar(object): pass # new-style
>>> Bar() < Foo()
False
在python 3.x中,比较的规则发生了变化,比如,将整型和字符串放在一起比较会报错。
>>> '10' > 5
Traceback (most recent call last):
File "", line 1, in
'10' > 5
TypeError: unorderable types: str() > int()