python functools total ordering

waylonwalker

Waylon Walker

Posted on January 14, 2022

python functools total ordering

functools.total_ordering makes adding all of six of the rich comparison operators to your custom classes much easier, and more likely that you remember all of them.

From the Docs: The class must define one of __lt__(), __le__(), __gt__(), or __ge_(). In addition, the class should supply an __eq__() method.

Total Ordering Docs

Here is an example using the Enum I was working on the other day.

from enum import Enum, auto from functools import total_ordering


@total_ordering
class LifeCycle(Enum):

    configure = auto()
    glob = auto()
    load = auto()
    pre_render = auto()
    render = auto()
    post_render = auto()
    save = auto()

    def __lt__(self, other):
        try:
            return self.value < other.value
        except AttributeError:
            return self.value < other

    def __eq__(self, other):
        try:
            return self.value == other.value
        except AttributeError:
            return self.value == other

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
waylonwalker
Waylon Walker

Posted on January 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related