Hi All,

Sending this to python-dev as I'm wondering if this was considered when the choice to have objects of different types raise a TypeError when ordered...

So, the concrete I case I have is implementing stable ordering for the python Range objects that psycopg2 uses. These have 3 attributes that can either be None or, for sake of argument, a numeric value.

To implement __lt__ in Python 2, I could do:

    def __lt__(self, other):
        if not isinstance(other, Range):
            return True
        return ((self._lower, self._upper, self._bounds) <
                (other._lower, other._upper, other._bounds))

Because None < 1 raises a TypeError, in Python 3 I have to do:

    def __lt__(self, other):
        if not isinstance(other, Range):
            return NotImplemented
        for attr in '_lower', '_upper', '_bounds':
            self_value = getattr(self, attr)
            other_value = getattr(other, attr)
            if self_value == other_value:
                pass
            elif self_value is None:
                return True
            elif other_value is None:
                return False
            else:
                return self_value < other_value
        return False

Am I missing something? How can I get this method down to a sane size?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to