Python Glossary Русский

all Python 3operatorfunction

operator.abs()

arithmetic

A function wrapper around the built-in abs(): it returns the absolute value. It is meant for places where a function is passed as a value.

Syntax

operator.abs(obj)

In its result it is identical to the built-in abs(): both call __abs__. The point is uniformity: code building an operation table such as {'+': operator.add, 'abs': operator.abs} takes every function from one module. For map(abs, data) no import is needed, the built-in abs is passed as a value just as well.

Examples

import operator
print(list(map(operator.abs, [-1, 2, -3])))  # → [1, 2, 3]
import operator
ops = {'neg': operator.neg, 'abs': operator.abs}
print(ops['abs'](-7.5))  # → 7.5

See also

Open in the full glossarydocs.python.orgReport an inaccuracy