Python In-Place Exponentiation Operator

Rate this post

Python provides the in-place exponentiation operator x **= y that raises x to the power of y using x ** y and assigns the result to the first operands variable name x. You can set up the in-place exponentiation behavior for your own class by overriding the magic “dunder” method __ipow__(self, other) in your class definition.

>>> x = 2
>>> x **= 3
>>> x
8

The expression x **= y is syntactical sugar for the longer-form x = x ** y:

>>> x = 2
>>> x = x ** 3
>>> x
8

Let’s explore some examples on different data types of the operands.

Integer Example

The **= operator on integer operands raises the first to the power of the second operand and stores it in the left-hand operands’ variable.

>>> x = 8
>>> x **= 2
>>> x
64

Float Example

If at least one of the operands is a float value, the result is also a float—float is infectious!

>>> x = 8
>>> x **= 2.0
>>> x
64.0

Incompatible Data Type

What if two operands have an incompatible data type—unlike floats and integers? For example, if you try to potentiate an integer and a list variable?

>>> x = 4
>>> x **= [1, 2]
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    x **= [1, 2]
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'list'

The result of incompatible addition is a TypeError. You can fix it by using only compatible data types for the in-place exponentiation operation.

Can you use the exponent operator on custom objects? Yes!

Python In-Place Exponentiation Magic Method

To use the in-place exponentiation operator **= on custom objects, define the __ipow__() method (“dunder method”, “magic method”) that takes two arguments self and other, updates the first argument self with the result of the addition, and returns the updated object.

In the following code, you calculate the power of two Data objects by combining their contents:

class Data:

    def __init__(self, data):
        self.data = data

    def __ipow__(self, other):
        self.data **= other.data
        return self

x = Data(2)
y = Data(3)

x **= y
print(x.data)
# 8

You can see that the content of the first operand is updated as a result of the in-place exponentiation operation.

Python In-Place Operators

In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3 is the same as x = x + 3 of first calculating the result of x +3 and then assigning it to the variable x.

OperatorNameShort ExampleEquivalent Long Example
=In-place Assignmentx = 3
+=In-place Additionx += 3x = x + 3
-=In-place Subtractionx -= 3x = x - 3
*=In-place Multiplicationx *= 3x = x * 3
/=In-place Divisionx /= 3x = x / 3
%=In-place Modulox %= 3x = x % 3
//=In-place Integer Divisionx //= 3x = x // 3
**=In-place Powerx **= 3x = x ** 3
&=In-place Bitwise Andx &= 3x = x & 3
|=In-place Bitwise Orx |= 3x = x | 3
^=In-place Bitwise XORx ^= 3x = x ^ 3
>>=In-place Bitwise Shift Rightx >>= 3x = x >> 3
<<=In-place Bitwise Shift Leftx <<= 5x = x << 5