マニュアルには次のように書かれています。
一般に
__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()
右側にある when が__int__()
呼び出されていないことに驚いています。私は何を間違っていますか?
追加__gt__()
するとこれは修正されますが、注文の最小要件が何であるかを理解していないことを意味します...
ありがとう、アンドリュー