Special Method Protocols

__neg__() — numeric protocol

Unary negation dispatches to this special method. Unary operations have no reflected fallback. The result is determined by the class; this example delegates to an integer.

Formal Syntax & Structure

python syntaxpython
def __neg__(self):

Return Value

object

Value produced by the hook.

Working Example

Example Snippetpython
class Number:    def __init__(self, value):        self.value = value    def __neg__(self):        return -self.valueprint(-Number(7))
Python Reference