ordering with duck typing in 3.1

andrew cooke andrew at acooke.org
Sat Apr 7 08:23:25 EDT 2012


hi,

please, what am i doing wrong here?  the docs say http://docs.python.org/release/3.1.3/library/stdtypes.html#comparisons "in general, __lt__() and __eq__() are sufficient, if you want the conventional meanings of the comparison operators" but i am seeing

>       assert 2 < three
E       TypeError: unorderable types: int() < IntVar()

with this test:


class IntVar(object):

    def __init__(self, value=None):
        if value is not None: value = int(value)
        self.value = value

    def setter(self):
        def wrapper(stream_in, thunk):
            self.value = thunk()
            return self.value
        return wrapper

    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

so what am i missing?

thanks,
andrew



More information about the Python-list mailing list