Python Glossary Русский

all Python 3operatorfunction

operator.pos

arithmetic

Returns +a (unary plus; for numbers the value itself, but it may call __pos__).

Syntax

operator.pos(a)

For int and float pos changes nothing, but it is not always a no-op: on decimal.Decimal unary plus applies the current context precision and rounds the value, and on collections.Counter it drops zero and negative counts. In your own class __pos__ defines the behaviour.

Examples

import operator
print(operator.pos(-3))   # → -3
print(operator.pos(-3) == +(-3))   # → True
print(operator.pos(True))   # → 1
print(list(map(operator.pos, [1, -2, 3])))   # → [1, -2, 3]
print(operator.pos("a"))   # → TypeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy