Hello guys.
So we are going to continue discussion with respect to Python.
And in this video we are going to discuss about operator overloading.
Now in our previous video when we were discussing about magic methods.
Right.
So here you will be also able to see some of the function or methods or magic methods, which is called
as underscore underscore equal underscore underscore.
This is nothing, but this is a default.
Uh, you can basically say this is a default method for any comparison or operation, right?
Like, uh, when we say, hey, uh, if you really want to do any comparison operation, we usually
use double equal to, right?
So if I want to overwrite that particular functionality, then I have to override this particular function.
Let's say uh I also use a lot of different different operation like one plus one two.
Right.
So one is an addition operation.
Now how do I probably take this plus and override this particular operation?
I will be showing you that if I am doing one minus one.
So this is a subtraction operation.
How we can actually override this.
So that is what we are basically going to do.
And along with this we know that okay I also have this kind of condition two L is greater than six right.
So why it is giving us true.
So this is nothing but this is uh greater than or equal to.
Can we override this particular operator also that we will be seeing.
And that is what we are going to see in this particular video, how we can perform operator overloading.
Some of the best example over here is that you have this underscore, underscore, add, underscore,
underscore this nothing it does, but it adds two objects using the plus operator.
Similarly you have for subtraction, you have for multiplication, you have for true div.
This is for the division operation.
Then you have equal sign compare if two objects are equal using the double equal to operator, and similarly
you have less than, you also have greater than uh which you can basically use underscore underscore
gt underscore underscore.
Right.
So that is what I'm actually going to show you each and everything over here.
So underscore underscore you can basically write it over here.
So let's say if I go ahead and write underscore underscore GT this is for the greater than if you want
to override that particular functionality.
Now let me just show you over here and we will try to uh, overload some of the operations with respect
to addition, subtraction and all.
Okay.
Uh, so first of all, I'm going to define my class, which is called as vector.
So this is my, uh, sorry, I will just go ahead and define my class, which is nothing but vector.
Now inside this vector I will be writing my init function.
So here you basically have init function.
And with respect to init function let's say I'm going to create two variables x comma y.
Now if I go ahead and write a self dot x is equal to x.
And uh here I'm going to write self dot y is equal to y.
Okay.
Now I'm going to just go ahead and add my add function over here.
So let me just go go ahead and use this definition add underscore underscore.
And here I have to probably give two parameters self comma other right.
Right.
So here what I'm doing I'm just giving one more additional parameter.
Now what I'm actually going to do over here is that uh, in this addition operation I will just change
change this entire operation itself.
So I'll say, hey, go ahead and return.
Okay.
Vector class over here.
And inside this vector class we'll give self dot x okay.
Plus other dot x okay.
I'm just putting some kind of operation.
Okay.
I'm doing some kind of operation over here.
Self dot x okay.
Not z x plus other dot x.
And here what I'm actually going to do again I'll go ahead and write self dot y plus other dot y.
That basically means what I'm actually going to do.
This other will be an another vector itself which will have x and y coordinate.
And I'm just summing it up with the x that I'm actually giving.
And along with the y coordinate that I'm actually giving.
Right.
So I'm just going to sum it up with respect to this particular other.
Right.
Similarly what I will do I will do the same thing for uh, let's say subtraction for multiplication.
And also here I will go ahead and define my another function.
And here you can see I'm doing subtracting over here I'm doing uh subtracting over here.
Right.
Other will be another vector okay.
We'll see to it once we properly initialize and call the function.
Now along with this, uh, what I am actually going to do is that, uh, I'm also going to make sure
to change my equal to functionality.
Okay.
I'm going to overload this particular operator itself.
So here with respect to equal to I'm saying return self dot x double equal to other dot x and self dot
y double equal to other dot y.
It will be either true or false okay, based on the comparison that is basically happening.
And finally, to represent this entire string, uh, we will be using underscore underscore r e p r.
And here we am just displaying the vector with self dot x and self dot y.
Okay.
Just a simple operation.
And just to show you that how I'm overriding each and everything.
So finally we go ahead and create objects of the vector class.
Okay.
And here we are just going to write v one is equal to vector two comma three.
Okay.
And then I will be going to create my vector v two.
And this will be nothing but four comma five okay.
Now this is my v one and v two vector.
Now let's see okay.
What kind of operation will basically happen if I go ahead and write print v1 plus v2?
We have already.
You can see that I've already changed this plus underscore add operation.
Right.
So if I go ahead and print it so what is the output that I'm actually getting.
See V1 is nothing.
But so here you can see that I'm adding both this.
Write two plus 463 plus five.
Right.
So that is what I'm printing it with respect to the addition operation.
So that is what is basically happening self dot x other dot x.
So in short I have modified this entire functionality for the addition of vectors.
That is the main purpose over here okay.
Please make sure that is the reason I had written initially the all the code.
This is nothing but I'm doing mathematical operation for vectors, which is not available right now.
So I have defined my own custom one here.
What we have done here, this is one coordinate x comma y and this is the other coordinate x comma y.
Right.
And when we do add operation this is going to get considered V2 is going to get considered as the other
option right.
Then I'm going to take this as x and y coordinate.
And this will be my other x and y coordinate.
And this is what we are getting over here.
And we will be able to add those vectors.
Right.
So next I will go ahead and write v one minus v two.
Similarly print v one multiplied by three okay.
So if I go ahead and execute it first of all v one minus v two.
What it is going to do self dot x multiplied by other.
So sorry this is multiplication.
So self dot x minus other dot x.
So we are just going to subtract it two minus four is two and three minus five is minus two.
Sorry two minus four is minus two and three minus five is minus two.
And here you will be able to see that with respect to multiplication operation I am just multiplying
with this particular number v one multiplied by three.
So I have x and y coordinate.
So x multiplied by other number over here three and y multiplied by three.
It is nothing but six comma nine right?
So this is how you can entirely overload all the operators based on your assumption.
You can take up different different operators and definitely make sure that you try to, uh, you know,
convert them based on your vectors or based on your requirements that you like.
I will also give you one more, uh, example, which you can do it as your assignment.
That is nothing but overloading operators for complex numbers.
You can just go ahead and explore this particular code.
Now it's time that you have to give some amount of time.
And here we have also used true div and see what all things we are basically doing.
If I talk about complex number, there will be an imaginary number.
So there will be a real number.
It will be an imaginary number.
And based on that we have written this code.
So Joe, just go ahead and explore this and let me know whether you are able to understand or not.
So finally, I would like to add some conclusion saying that, uh, magic methods and operator overloading
are some amazing, powerful feature, uh, in Python, allowing you to completely customize the operator
functionalities itself based on your application development that you really want.
Right?
So yes, this was it from my side.
I hope you liked this particular video.
I will see you in the next video.
Thank you.
Take.