Back to Browse

Operator Overloading in Python

128 views
Dec 5, 2024
11:46

#python #oop #operator #overloading In this lecture, we're going to dive into an exciting and powerful concept in Python: Operator Overloading. This is what allows Python classes to redefine the behavior of standard operators like +, -, and ==. You might have noticed that Python already lets us add numbers, compare strings, and multiply lists, but what if we want to make our custom objects behave this way too? That's where operator overloading comes in. We'll start with the basics of operator overloading and then dive into specific cases like Addition using the __add__ method, and Subtraction with the __sub__ method. Afterward, we'll see how to handle Multiplication by implementing the __mul__ method. We'll also cover comparisons, including checking equality with the __eq__ method and comparing objects using the __lt__ method. To tie it all together, we'll use a practical example, a Vector Class representing points in 3D space with x, y, and z coordinates. We'll implement operator overloading for addition, subtraction, scalar multiplication, and comparisons. So, by the end of this lecture, we'll understand how to overload operators to work with our custom objects. Let's start with a quick introduction to the idea. Operator overloading allows us to redefine how operators work for user-defined objects. Python already knows what to do when we use + between numbers, but when it comes to custom objects like our Vector class, we need to tell Python what +, -, or == should do. Python offers special methods, also known as dunder methods, such as __add__, __sub__, and __eq__, which we can implement in our classes to define how certain operations behave. These methods guide Python on what to do when operators are used with our custom objects. Now, let's start implementing our Vector class to see how this works in practice. For that, we'll create a new Python file, and name it 'vector.py'. In the file, we'll define our Vector class that represents a 3D vector with x, y and z components. Our constructor takes three arguments as the coordinates of our vector. First of all, let's override the __str__ method to provide a readable string representation of our vector. This method is automatically called when we print the object or use str() on it. It returns the vector's components in a formatted string. Now, let's create two instances of our Vector class. After creating these vectors, we'll use print statements to display them. Since we've overridden the __str__ method, Python will automatically format the vector into the (x, y, z) style when we print it. Let's run the script and verify it. As a result, we'll see the formatted output showing the coordinates of each vector. Now, let's add support for adding two vectors together. We know that mathematically, vector addition is component-wise. To do that, we define the __add__ method. It's what Python will call when we use the + operator between two vectors. Here, the parameters 'self' and 'other' represent two vector objects that we want to add together. 'self' refers to the current instance of the class, and represents the vector object on the left side of the + operator. 'other' refers to the vector object that we're adding to self, meaning the vector on the right side of the + operator. Inside the method, we add the x, y, and z components of both vectors, and return a new Vector object with the resulting coordinates. This allows us to perform component-wise addition of the two vectors. Next, we'll add the two vectors, v1 and v2, using the + operator. The result will be a new vector, which we'll store in v3. Thanks to the __add__ method we defined earlier, Python knows how to handle this operation for our Vector objects. When we print v3, Python will once again call the __str__ method to format the output, showing the coordinates of the new vector. If we run the code at this point, We'll see the result of the addition. Next, we want to implement subtraction in the same way as addition. Vector subtraction is also component-wise. Let's define the __sub__ method. When we subtract two vectors, we'll get a new vector representing the difference between their components. Let's see this in action by subtracting v2 from v1. After running this, We should see v4 as the result of vector subtraction. Now, what about multiplying a vector by a scalar? We might want to multiply each component of a vector by a constant. For this, we'll overload the __mul__ method. This will allow us to multiply a vector by a scalar. Let's try it by multiplying v1 by 3 and assigning the result to v5. If we execute this code, We'll observe the result of such a scalar multiplication. Note that the __mul__ method only handles multiplication when the vector is on the left side of the operator, like v1 * 3. However, if we try to multiply the scalar from the left side, such as 3 * v1, Python won't automatically know how to handle it. To fix this, we need to implement the __rmul__ method, which allow...

Download

1 formats

Video Formats

360pmp415.9 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Operator Overloading in Python | NatokHD