On this page
  1. functools.total_ordering
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

functools.total_ordering

Prev: functools.singledispatchmethod | Up: functools | Next: functools.update_wrapper
Rel: functools.cache, functools.cached_property, functools.cmp_to_key, functools.lru_cache
Ref: docs.python.org — functools.total_ordering
Tags: #public #python3 #stdlib #functools


Import

import functools
from functools import total_ordering

Signature & Overview

functools.total_ordering(cls)

Class decorator that fills in missing ordering methods

Usage

from functools import total_ordering

@total_ordering
class Version:
    def __init__(self, major: int, minor: int) -> None:
        self.key = (major, minor)

    def __eq__(self, other: object) -> bool:
        return isinstance(other, Version) and self.key == other.key

    def __lt__(self, other: "Version") -> bool:
        if not isinstance(other, Version):
            return NotImplemented
        return self.key < other.key

assert Version(1, 2) <= Version(1, 2) < Version(2, 0)
assert Version(2, 0) >= Version(1, 2)

Gotchas

REPL Inspect

from functools import total_ordering
help(total_ordering)

Linked from

Related notes