Special Method Protocols

__ne__() — comparison protocol

Customize not equal (!=). The interpreter may try the reflected comparison when the first hook declines; comparison hooks may return non-Boolean results. Equality falls back to identity when both sides decline; inequality falls back to non-identity.

Formal Syntax & Structure

python syntaxpython
def __ne__(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 __ne__(self, other):        if not isinstance(other, Rank):            return NotImplemented        return self.value != other.valueprint(Rank(4) != Rank(5))
Python Reference