__pos__Easy Examples

Defines behavior for the unary + positive operator

Implementing __pos__

Basic implementation of __pos__ in a class.

python
class Example:
    def __pos__(self):
        return "Example __pos__"

obj = Example()
print(obj)

__pos__ defines behavior for the unary + positive operator. Implementing it lets you customize how Python interacts with your objects.

__pos__ in action

Seeing __pos__ called by Python's built-in operations.

python
# How Python calls __pos__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __pos__(self):
        print(f"__pos__ was called!")
        return self

d = Demo(42)
# This triggers __pos__:
print(d)

Python automatically calls __pos__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground