Special Method Protocols

__abs__() — numeric protocol

Absolute magnitude 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 __abs__(self):

Return Value

object

Value produced by the hook.

Working Example

Example Snippetpython
class Number:    def __init__(self, value):        self.value = value    def __abs__(self):        return abs(self.value)print(abs(Number(-7)))
Python Reference