【发布时间】:2012-04-07 13:35:21
【问题描述】:
在the manual 中说:
一般来说,
__lt__()和__eq__()就足够了,如果你想要 比较运算符的常规含义
但我看到了错误:
> assert 2 < three
E TypeError: unorderable types: int() < IntVar()
当我运行这个测试时:
from unittest import TestCase
class IntVar(object):
def __init__(self, value=None):
if value is not None: value = int(value)
self.value = value
def __int__(self):
return self.value
def __lt__(self, other):
return self.value < other
def __eq__(self, other):
return self.value == other
def __hash__(self):
return hash(self.value)
class DynamicTest(TestCase):
def test_lt(self):
three = IntVar(3)
assert three < 4
assert 2 < three
assert 3 == three
我很惊讶当IntVar() 在右边时,__int__() 没有被调用。我做错了什么?
添加 __gt__() 可以解决此问题,但这意味着我不明白订购的最低要求是什么...
谢谢, 安德鲁
【问题讨论】:
-
如果你查看rich comparison method docs,它特别提到了这种行为——
There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection. Arguments to rich comparison methods are never coerced. -
@agf:答案应该在答案中,而不是在 cmets 中。
-
@EthanFurman 文档不会像 Sven 的回答那样具体案例,而且 IMO 有必要将其作为答案发布,而不仅仅是评论。
标签: python python-3.x duck-typing