TUTORIAL

Operator Overloading

This section introduces operator overloading in Python

On this page

Operator Overloading

Everything in Python is an object, and each object has some special internal methods used to interact with other objects. Usually, these methods follow the __action__ naming convention.

You can overload any of these methods. This is commonly used for operator overloading in Python. Below is an example of operator overloading using the Python data model. The Vector class creates a simple vector with two variables. We will use operator overloading to support mathematical operations between two vectors.

class Vector(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, v):
        # Addition with another vector.
        return Vector(self.x + v.x, self.y + v.y)

    def __sub__(self, v):
        # Subtraction with another vector.
        return Vector(self.x - v.x, self.y - v.y)

    def __mul__(self, s):
        # Multiplication with a scalar.
        return Vector(self.x * s, self.y * s)

    def __div__(self, s):
        # Division with a scalar.
        float_s = float(s)
        return Vector(self.x / float_s, self.y / float_s)

    def __floordiv__(self, s):
        # Division with a scalar (value floored).
        return Vector(self.x // s, self.y // s)

    def __repr__(self):
        # Print friendly representation of Vector class. Else, it would
        # show up like, <__main__.Vector instance at 0x01DDDDC8>.
        return "<Vector (%f, %f)>" % (
            self.x,
            self.y,
        )


a = Vector(3, 5)
b = Vector(2, 7)

print a + b  # Output: <Vector (5.000000, 12.000000)>
print b - a  # Output: <Vector (-1.000000, 2.000000)>
print b * 1.3  # Output: <Vector (2.600000, 9.100000)>
print a // 17  # Output: <Vector (0.000000, 0.000000)>
print a / 17  # Output: <Vector (0.176471, 0.294118)>

Through operator overloading, we can make a class perform some mathematical operations. If these operations are called frequently, this saves the steps of implementing them through functions.

For the specific list of operators that support overloading, see the official manual.