Python In-Place Assignment Operators – Be on the Right Side of Change (original) (raw)

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.

Operator Name Short Example Equivalent Long Example
= In-place Assignment x = 3
+= In-place Addition x += 3 x = x + 3
-= In-place Subtraction x -= 3 x = x - 3
*= In-place Multiplication x *= 3 x = x * 3
/= In-place Division x /= 3 x = x / 3
%= In-place Modulo x %= 3 x = x % 3
//= In-place Integer Division x //= 3 x = x // 3
**= In-place Power x **= 3 x = x ** 3
&= In-place Bitwise And x &= 3 x = x & 3
|= In-place Bitwise Or x |= 3 x = x | 3
^= In-place Bitwise XOR x ^= 3 x = x ^ 3
>>= In-place Bitwise Shift Right x >>= 3 x = x >> 3
<<= In-place Bitwise Shift Left x <<= 5 x = x << 5

You can watch me go over all of these operators in the following video:

We’ll rush over all in-place operators one-by-one next!

Python In-Place Addition

Python provides the operator x += y to add two objects in-place by calculating the sum x + y and assigning the result to the first operands variable name x. You can set up the in-place addition behavior for your own class by overriding the magic “dunder” method __iadd__(self, other) in your class definition.

x = 1 x += 2 x 3

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

x = 1 x = x + 2 x 3

Related Tutorial: Python In-Place Addition

Python In-Place Subtraction

Python provides the operator x -= y to subtract two objects in-place by calculating the difference x - y and assigning the result to the first operands variable name x. You can set up the in-place subtraction behavior for your own class by overriding the magic “dunder” method __isub__(self, other) in your class definition.

x = 3 x -= 2 x 1

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

x = 3 x = x - 2 x 1

Related Tutorial: Python In-Place Subtraction

Python In-Place Multiplication

Python provides the operator x *= y to multiply two objects in-place by calculating the product x * y and assigning the result to the first operands variable name x. You can set up the in-place multiplication behavior for your own class by overriding the magic “dunder” method __imul__(self, other) in your class definition.

x = 2 x *= 3 x 6

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

x = 2 x = x * 3 x 6

Related Tutorial: Python In-Place Multiplication

Python In-Place Division

Python’s in-place division operator x /= y divides two objects in-place by calculating x / y and assigning the result to the first operands variable name x. Set up in-place division for your own class by overriding the magic “dunder” method __truediv__(self, other) in your class definition.

x = 4 x /= 2 x 2

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

x = 4 x = x / 2 x 2

Related Tutorial: Python In-Place Division

Python In-Place Modulo

Python provides the operator x %= y to calculate the modulo operation x % y, and assign the result in-place to the first operands variable x. You can set up the in-place modulo behavior for your own class by overriding the magic “dunder” method __imod__(self, other) in your class definition.

x = 9 x %= 4 x 1

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

x = 9 x = x % 4 x 1

Related Tutorial: Python In-Place Modulo

Python In-Place Integer Division

Python’s in-place integer division operator x //= y divides two objects in-place by calculating x // y and assigning the result to the first operands variable name x. Set up in-place integer (or floor) division for your own class by overriding the magic “dunder” method __floordiv__(self, other) in your class definition.

x = 5 x //= 2 x 2

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

x = 5 x = x // 2 x 2

Related Tutorial: Python In-Place Integer Division

Python In-Place Exponentiation

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

Related Tutorial: Python In-Place Exponentiation

Python In-Place Bitwise AND

Python’s in-place bitwise AND operator x &= y calcualtes bitwise-and x & y and assigns the result to the first operand x. To set it up for your own class, override the magic “dunder” method __iand__(self, other) in your class definition.

x = 1 x &= 2 x 0

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

x = 1 x = x & 2 x 3

Related Tutorial: Python In-Place Bitwise AND

Python In-Place Bitwise OR

Python’s A |= B applies the | operator in place. Thus, it is semantically identical to the longer-form version A = A | B of first performing the operation A | B and then assigning the result to the variable A.

The following minimal example creates two Boolean variables A and B and performs the in-place B |= A operation to perform a logical OR operation B | A and assigning the result to the first operand B that becomes True:

A = True B = False B |= A B True

In this example, you’ve seen this in-place operation on Boolean operands. But the | operator is overloaded in Python. The three most frequent use cases for the | and |= operators are the following:

Related Tutorial: Python In-Place Bitwise OR

Python In-Place Bitwise XOR

Python’s in-place bitwise XOR operator x ^= y calcualtes bitwise XOR x `^` y and assigns the result to the first operand x. To set this up for your own class, override the magic “dunder” method __ixor__(self, other) in your class definition.

x = 1 x ^= 2 x 3

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

x = 1 x = x ^ 2 x 3

Related Tutorial: Python In-Place Bitwise XOR

Python In-Place Bitwise Right-Shift

Python’s in-place bitwise right-shift operator x >>= y calculates the right-shift operation x >> y, and assigns the result to the first operands variable name x. You can set up the in-place right-shift behavior in your own class by overriding the magic “dunder” method __irshift__(self, other) in your class definition.

x = 8 x >>= 2 x 2

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

x = 8 x = x >> 2 x 2

Related Tutorial: Python In-Place Bitwise Right-Shift

Python In-Place Bitwise Left-Shift

Python’s in-place bitwise left-shift operator x <<= y calculates the left-shift operation x << y, and assigns the result to the first operands variable name x. You can set up the in-place left-shift behavior in your own class by overriding the magic “dunder” method __ilshift__(self, other) in your class definition.

x = 8 x <<= 2 x 32

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

x = 8 x = x << 2 x 32

Related Tutorial: Python In-Place Bitwise Left-Shift

Python In-Place Magic Methods

The following table provides the names of the magic methods you need to define to enable in-place operators on your custom class:

Method Name Description
__iadd__(self, other) In-place addition
__isub__(self, other) In-place subtraction
__imul__(self, other) In-place multiplication
__truediv__(self, other) In-place and normal division
__imod__(self, other) In-place modulo
__floordiv__(self, other) In-place and normal integer division
__ipow__(self, other) In-place exponentiation
__iand__(self, other) In-place bitwise AND
__ior__(self, other) In-place bitwise OR
__ixor__(self, other) In-place bitwise XOR
__irshift__(self, other) In-place right-shift
__ilshift__(self, other) In-place left-shift

In the following code example, we create a custom class Data and define our “magic” double-underscore methods so that we can perform in-place computations on objects of this class.

class Data:

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

def __iadd__(self, other):
    self.data += other.data
    return self

def __isub__(self, other):
    self.data -= other.data
    return self

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

def __truediv__(self, other):
    self.data /= other.data
    return self

def __imod__(self, other):
    self.data %= other.data
    return self

def __floordiv__(self, other):
    self.data //= other.data
    return self

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

def __iand__(self, other):
    self.data &= other.data
    return self

def __ior__(self, other):
    self.data |= other.data
    return self

def __ixor__(self, other):
    self.data ^= other.data
    return self

def __irshift__(self, other):
    self.data >>= other.data
    return self

def __ilshift__(self, other):
    self.data <<= other.data
    return self

Let’s try these out!

In-Place Addition

x = Data(3) y = Data(2) x += y print(x.data)

5

In-Place Subtraction

x = Data(3) y = Data(2) x -= y print(x.data)

1

In-Place Multiplication

x = Data(3) y = Data(2) x *= y print(x.data)

6

In-Place Division

x = Data(3) y = Data(2) x /= y print(x.data)

1.5

In-Place Modulo

x = Data(3) y = Data(2) x %= y print(x.data)

1

In-Place Integer Division

x = Data(3) y = Data(2) x //= y print(x.data)

1

In-Place Power

x = Data(3) y = Data(2) x **= y print(x.data)

9

In-Place Bitwise AND

x = Data(3) y = Data(2) x &= y print(x.data)

2

In-Place Bitwise OR

x = Data(3) y = Data(2) x |= y print(x.data)

3

In-Place Bitwise XOR

x = Data(3) y = Data(2) x ^= y print(x.data)

1

In-Place Bitwise Right-Shift

x = Data(3) y = Data(2) x >>= y print(x.data)

0

In-Place Bitwise Left-Shift

x = Data(3) y = Data(2) x <<= y print(x.data)

12