【发布时间】:2012-01-06 19:10:21
【问题描述】:
下面这段代码
class point:
def __init__(self, x, y):
self.x = x
self.y = y
def dispc(self):
return ('(' + str(self.x) + ',' + str(self.y) + ')')
def __cmp__(self, other):
return ((self.x > other.x) and (self.y > other.y))
在 Python 2 中运行良好,但在 Python 3 中出现错误:
>>> p=point(2,3)
>>> q=point(3,4)
>>> p>q
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: point() > point()
它仅适用于== 和!=。
【问题讨论】:
-
__cmp__在 Python 2 中已经被破坏了。
标签: python python-3.x python-2.x partial-ordering