Python __idiv__() Magic Method

5/5 - (1 vote)

The Python __idiv__() magic method overrides the in-place division operation for a custom object in Python 2.

In Python 3, it was replaced by the __itruediv__() method for a /= b and __ifloordiv__() dunder methods for a //= b.

  • The Python __itruediv__() method is called to implement the in-place true division operation /=.
  • The Python __ifloordiv__() method implements the in-place integer floor division operation //=.

Syntax

object.__idiv__(self, other)

The Python version 2 __idiv__() magic method implements the in-place division operation x /= y that calculates the division operation x / y, and assigns the result to the first operands variable x. This operation is also called augmented arithmetic assignment. The method simply returns the new value to be assigned to the first operand.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Basic Example Overriding __idiv__

In the following code example (that only runs in Python 2 and not in Python 3+), you create a class Data and define the magic method __idiv__(self, other).

  • The “self” argument is the default argument of each method and it refers to the object on which it is called—in our case, the first operand of the in-place operation.
  • The “other” argument of the in-place method refers to the second operand, i.e., y in the in-place operation x /= y.

The return value of the operation returns a dummy string 'finxter 42' to be assigned to the first operand. In practice, this would be the result of the in-place true division operation.

# Python 2
class Data:
    def __idiv__(self, other):
        return 'finxter 42'


x = Data()
y = Data()

x /= y

print(x)
# finxter 42

Note that this would look different for Python 3:

# Python 3+
class Data:
    def __itruediv__(self, other):
        return 'finxter 42'

In-Place True Division /= Without __idiv__()

To support in-place true division on a custom class, you don’t have to overwrite the in-place __idiv__() method. Because if the method is not defined, Python will fall back to the normal __div__() method and assign its result to the first operand.

Here’s an example:

class Data:
    def __div__(self, other):
        return 'finxter 42'


x = Data()
y = Data()

x /= y

print(x)
# finxter 42

Even though the __idiv__() method is not defined, the in-place true division operation x /= y still works due to the __div__() “fallback” magic method!

In-Place True Division /= Without __itruediv__() and __truediv__()

To support in-place true division x /= y on a custom class, you don’t even have to overwrite any of the x.__idiv__(y) or x.__div__(y) methods. If both are not defined, Python falls back to the reverse y.__rdiv__(x) method and assigns its result to the first operand.

Here’s an example where you create a custom class for the first operand that doesn’t support the true division operation. Then you define a custom class for the second operand that defines the __rtruediv__() method. For the in-place operation, Python falls back to the __rtruediv__() method defined on the second operand and assigns it to the first operand x:

class Data_1:
    pass

class Data_2:
    def __rdiv__(self, other):
        return 'finxter 42'

x = Data_1()
y = Data_2()

x /= y

print(x)
# finxter 42

Background True Division

In Python 3, the single front-slash “/” is a float division operator that returns a float value as a result. For example, the expression 10/4 returns 2.5 instead of 2, and 4/2 returns 2.0 instead of 2.

>>> # Python 3
>>> 10/4
2.5
>>> 4/2
2.0

Be careful to use the most updated type of Python available. For example, Python 2.0 returns an integer instead of a float value for the / operator. Also when we perform division in Python we want to be careful what value we divide by. We find that if we divide by a whole number, it will round to an integer.

>>> 10 / 90
0

You can find full tutorials on related operators (including videos) here:

Related Video

References:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!