Special Method Protocols

__pow__() — numeric protocol

Implement the left operand hook for exponentiation (**). Return NotImplemented for an unsupported operand type to permit reflected dispatch. Three-argument pow() supplies the modulus; two-argument power does not.

Formal Syntax & Structure

python syntaxpython
def __pow__(self, other, modulo=None):

Parameters & Attributes

NameTypeDescription
otherobjectOther operand.
moduloobject | NoneOptional modulus for three-argument pow().

Return Value

object | NotImplementedType

Operation result, or NotImplemented to decline.

Working Example

Example Snippetpython
class Number:    def __init__(self, value):        self.value = value    def __pow__(self, other, modulo=None):        if not isinstance(other, int):            return NotImplemented        return pow(self.value, other, modulo) if modulo is not None else self.value ** otherprint(Number(2) ** 3)
Python Reference