decorators#
Provides a decorator to add total ordering to a class using a _compare method.
This is useful in Python 3 when you want to define a single _compare() method and automatically get all comparison methods.
Functions
Decorator that adds total ordering to a class by defining all comparison operators using the _compare(other) method. |
- 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