Special Method Protocols

__lt__() — comparison protocol

Customize less than (<). The interpreter may try the reflected comparison when the first hook declines; comparison hooks may return non-Boolean results. Ordering raises TypeError when neither operand supplies a result.

Formal Syntax & Structure

python syntaxpython
def __lt__(self, other):

Parameters & Attributes

NameTypeDescription
otherobjectOther operand.

Return Value

object | NotImplementedType

Comparison result or NotImplemented.

Working Example

Example Snippetpython
class Rank:    def __init__(self, value):        self.value = value    def __lt__(self, other):        if not isinstance(other, Rank):            return NotImplemented        return self.value < other.valueprint(Rank(2) < Rank(4))
Python Reference