+x
Looks like dead code, and on int and float it is. It survives because __pos__ is a hook — Decimal and Counter both use it for something real.
Demo
On ordinary numbers unary plus does nothing at all — a negative stays negative, and the value comes back unchanged. That is the point worth internalising: it does NOT make a number positive. Its reason for existing is that it calls __pos__, and a few standard-library types use that hook to do genuinely useful work.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | number | yes | Operand. Any type defining __pos__ works; str and list do not. |
Return value
number — The operand, generally unchanged. Calls __pos__, which some types use to do real work.
Common patterns
from collections import Counter c = Counter(a=3, b=-1, c=0) positive_only = +c # Counter({"a": 3})
from decimal import Decimal, localcontext with localcontext() as ctx: ctx.prec = 3 rounded = +Decimal("1.23456")
expr = f"{sign}{value}" # sign is "+" or "-"
Examples
Pitfalls
+(-5)
abs(-5)
from collections import Counter c = Counter(a=3, b=-1) c # without the +
+c
+'abc'
'abc'+True
TrueWhen to use
- Filtering a Counter down to positive counts
- Forcing Decimal context rounding onto a value
- Generated or templated code where a leading sign keeps the shape uniform
- Making a number positive → abs
- Ordinary arithmetic on int and float — it does nothing
- Sequences, where it raises rather than being ignored
Notes
FAQ
It exists so types can hook __pos__. Counter uses it to drop non-positive counts, and Decimal uses it to apply the current context precision. On plain numbers it is genuinely a no-op — the operator is there for the types that need it.
+Counter(a=3, b=-1) # Counter({"a": 3})