_mirrored_op = {'__eq__': '__eq__', '__ne__': '__ne__', '__lt__': '__ge__',
    '__le__': '__gt__', '__ge__': '__lt__', '__gt__': '__le__'}

def richcmp(v, w, op):
    checked_reverse = 0
if type(v) != type(w) and issubclass(type(w), type(v)) and hasattr(w, op):
    checked_reverse = 1
res = getattr(w, _mirrored_op[op])(v)
if res is not NotImplemented:
    return res
if hasattr(v, op):
    res = getattr(v, op)(w)
if res is not NotImplemented:
    return res
if not checked_reverse and hasattr(w, op):
    res = getattr(w, _mirrored_op[op])(v)
if res is not NotImplemented:
    return res
if op == '__eq__':
    res = v is w
elif op == '__ne__':
    res = v is not w
else:
    raise TypeError('sth')
return res
Возврат NotImplemented из __eq__