total_ordering_from_compare#
- total_ordering_from_compare(cls)#
Decorator that adds total ordering to a class by defining all comparison operators using the _compare(other) method.
- The decorated class must implement:
def _compare(self, other) -> int
- Parameters:
cls (type[T]) – A class with a _compare method.
- Returns:
The same class with comparison methods __eq__, __ne__, __lt__, __le__, __gt__, __ge__.
- Return type:
type[T]
Example
@total_ordering_from_compare class MyObject:
- def __init__(self, value):
self.value = value
- def _compare(self, other):
return self.value - other.value