[Python-3000] Need closure on __cmp__ removal

hashcollision hashcollision at gmail.com
Tue Jan 8 05:48:17 CET 2008


>
> But the biggest thing missing is precise semantics. Saying "exactly
> the same semantics as with Python 2.5" doesn't cut it (those semantics
> are incredibly hairy and sometimes surprising, and their
> implementation was a nightmare -- I've rarely been as relieved as when
> I was able to cut those out of the implementation).


Why is that so? I might be being naive, but what is wrong with something
like this:

def cmp(a, b):
    if hasattr(a, '__cmp__'):
        return a.__cmp__(b)
    elif hasattr(b, '__cmp__'):
        return -1 * b.__cmp__(a)
    elif hasattr(a, '__le__') and hasattr(a, '__ge__'):
        x = a <= b
        y = a >= b
        if x and y:
            return 0
        elif x:
            return -1
        elif y:
            return 1
    elif hasattr(b, '__le__') and hasattr(b, '__ge__'):
        x = b <= a
        y = b >= a
        if x and y:
            return 0
        elif x:
            return 1
        elif y:
            return -1
    elif hasattr(a, '__eq__'):
        if a == b:
            return 0
        if hasattr(a, '__lt__'):
            if a < b:
                return -1
            else:
                return 1
        elif hasattr(a, '__gt__'):
            if a > b:
                return 1
            else:
                return -1
        else:
            raise NotImplemented()
    elif hasattr(b, '__eq__'):
        if a == b:
            return 0
        if hasattr(b, '__lt__'):
            if b < a:
                return 1
            else:
                return -1
        elif hasattr(b, '__gt__'):
            if b > a:
                return -1
            else:
                return 1
        else:
            raise NotImplemented()
    else:
        raise NotImplemented()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20080107/7b6fdbde/attachment-0001.htm 


More information about the Python-3000 mailing list