ZetCode

Python __pos__ Method

Last modified November 18, 2026 Python 3.15

The unary plus operator calls __pos__ on its operand. For Python's built-in numbers it is an identity operation, but a custom class may give it a different meaning, such as normalizing a value to be positive. This article covers the basic hook, built-in identity behaviour, absolute-value wrappers, vectors, an int subclass, currency values and operator.pos.

Basic unary plus

The hook takes only self. Printing a message makes the call visible; returning self mirrors what built-in numbers do.

basic_pos.py
class Number:
    def __init__(self, value):
        self.value = value

    def __pos__(self):
        print("__pos__ called")
        return self

    def __repr__(self):
        return f"Number({self.value})"


num = Number(5)
result = +num
print(result)
print(result is num)

The operator invoked the hook once and the returned object was the original instance. Returning self is the conventional behaviour because unary plus normally changes nothing.

$ python basic_pos.py
__pos__ called
Number(5)
True

Built-in numbers return themselves

For int, float and other built-in numerics, unary plus returns the very same object. There is no copying or conversion.

builtin_pos.py
x = 5
print(+x is x)
print(+3.5)
print(int.__pos__(x))

The identity test is True and the value is unchanged. Calling the special method directly produces the same result as the operator.

$ python builtin_pos.py
True
3.5
5

Normalizing a value

A class can choose a more useful meaning: here unary plus returns a new wrapper holding the absolute value. The original stays unchanged.

absolute_pos.py
class PositiveNumber:
    def __init__(self, value):
        self.value = value

    def __pos__(self):
        return PositiveNumber(abs(self.value))

    def __repr__(self):
        return f"PositiveNumber({self.value})"


n = PositiveNumber(-5)
print(+n)
print(+PositiveNumber(7))
print(n)

Both a negative and a positive input produce a non-negative result, while the original object keeps its -5. Overriding a familiar operator this way should be documented clearly.

$ python absolute_pos.py
PositiveNumber(5)
PositiveNumber(7)
PositiveNumber(-5)

Complete unary operators on a vector

Classes that model mathematical values often implement __pos__ and __neg__ together so the two unary operators have consistent meanings.

vector_unary.py
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __pos__(self):
        return Vector(+self.x, +self.y)

    def __neg__(self):
        return Vector(-self.x, -self.y)

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"


v = Vector(2, -3)
print(+v)
print(-v)

Unary plus leaves the components as they were, while unary minus flips their signs. Both return new vectors rather than changing v.

$ python vector_unary.py
Vector(2, -3)
Vector(-2, 3)

Extending a built-in numeric type

A subclass of int can override __pos__ and still use the parent's implementation through super(). The example adds one to the value the parent returns.

my_int.py
class MyInt(int):
    def __pos__(self):
        print("Custom __pos__ called")
        return MyInt(super().__pos__() + 1)


num = MyInt(5)
result = +num
print(result)
print(type(result).__name__)

int.__pos__ returned the original 5, the subclass added 1, and the result remained a MyInt. This is a deliberate change from the usual identity behaviour, so it is printed explicitly in the hook.

$ python my_int.py
Custom __pos__ called
6
MyInt

Currency display values

In finance, unary plus can convert a debt into a positive display amount without altering the stored value used in calculations.

currency.py
class Currency:
    def __init__(self, amount):
        self.amount = amount

    def __pos__(self):
        return Currency(abs(self.amount))

    def __repr__(self):
        return f"${self.amount:.2f}"


debt = Currency(-100.50)
print(+debt)
print(debt)

The displayed value is positive while the original still records the debt. This keeps presentation separate from the underlying calculation.

$ python currency.py
$100.50
$-100.50

The operator.pos function

operator.pos(a) is the functional form of +a. It calls the same hook and is useful when a callable is required.

operator_pos.py
import operator


class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __pos__(self):
        return Vector(+self.x, +self.y)

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"


print(operator.pos(Vector(2, 3)))
print(operator.pos(-4))

The custom vector used its hook and the built-in integer kept its identity behaviour. Both forms give the same result as the + operator.

$ python operator_pos.py
Vector(2, 3)
-4

Best practices

A few rules keep + predictable:

Source

object.__pos__ - Python documentation

Emulating numeric types - Python documentation

operator.pos - Python documentation

In this article we have implemented the __pos__ method. We have covered the unary plus dispatch, built-in identity behaviour, normalization, vectors, an int subclass, currency values and operator.pos. For related topics, see Python __neg__ method, Python __abs__ method, and Python operators.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Python tutorials.