__pow__Easy Examples

Defines behavior for the ** power operator

Implementing __pow__

Basic implementation of __pow__ in a class.

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

obj = Example()
print(obj)

__pow__ defines behavior for the ** power operator. Implementing it lets you customize how Python interacts with your objects.

__pow__ in action

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

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

    def __pow__(self, other):
        print(f"__pow__ was called!")
        return self

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

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

Want to try these examples interactively?

Open Easy Playground