1
00:00:00,000 --> 00:00:01,000
Hello guys.

2
00:00:01,000 --> 00:00:03,000
So we are going to continue discussion with respect to Python.

3
00:00:03,000 --> 00:00:06,000
And in this video we are going to discuss about operator overloading.

4
00:00:06,000 --> 00:00:10,000
Now in our previous video when we were discussing about magic methods.

5
00:00:10,000 --> 00:00:10,000
Right.

6
00:00:10,000 --> 00:00:15,000
So here you will be also able to see some of the function or methods or magic methods, which is called

7
00:00:15,000 --> 00:00:18,000
as underscore underscore equal underscore underscore.

8
00:00:18,000 --> 00:00:20,000
This is nothing, but this is a default.

9
00:00:20,000 --> 00:00:26,000
Uh, you can basically say this is a default method for any comparison or operation, right?

10
00:00:26,000 --> 00:00:31,000
Like, uh, when we say, hey, uh, if you really want to do any comparison operation, we usually

11
00:00:31,000 --> 00:00:32,000
use double equal to, right?

12
00:00:32,000 --> 00:00:38,000
So if I want to overwrite that particular functionality, then I have to override this particular function.

13
00:00:38,000 --> 00:00:43,000
Let's say uh I also use a lot of different different operation like one plus one two.

14
00:00:43,000 --> 00:00:44,000
Right.

15
00:00:44,000 --> 00:00:46,000
So one is an addition operation.

16
00:00:46,000 --> 00:00:50,000
Now how do I probably take this plus and override this particular operation?

17
00:00:50,000 --> 00:00:53,000
I will be showing you that if I am doing one minus one.

18
00:00:53,000 --> 00:00:55,000
So this is a subtraction operation.

19
00:00:55,000 --> 00:00:57,000
How we can actually override this.

20
00:00:57,000 --> 00:00:59,000
So that is what we are basically going to do.

21
00:00:59,000 --> 00:01:05,000
And along with this we know that okay I also have this kind of condition two L is greater than six right.

22
00:01:05,000 --> 00:01:07,000
So why it is giving us true.

23
00:01:07,000 --> 00:01:10,000
So this is nothing but this is uh greater than or equal to.

24
00:01:10,000 --> 00:01:13,000
Can we override this particular operator also that we will be seeing.

25
00:01:13,000 --> 00:01:17,000
And that is what we are going to see in this particular video, how we can perform operator overloading.

26
00:01:17,000 --> 00:01:23,000
Some of the best example over here is that you have this underscore, underscore, add, underscore,

27
00:01:23,000 --> 00:01:28,000
underscore this nothing it does, but it adds two objects using the plus operator.

28
00:01:28,000 --> 00:01:31,000
Similarly you have for subtraction, you have for multiplication, you have for true div.

29
00:01:31,000 --> 00:01:33,000
This is for the division operation.

30
00:01:33,000 --> 00:01:38,000
Then you have equal sign compare if two objects are equal using the double equal to operator, and similarly

31
00:01:38,000 --> 00:01:44,000
you have less than, you also have greater than uh which you can basically use underscore underscore

32
00:01:44,000 --> 00:01:46,000
gt underscore underscore.

33
00:01:46,000 --> 00:01:46,000
Right.

34
00:01:46,000 --> 00:01:50,000
So that is what I'm actually going to show you each and everything over here.

35
00:01:50,000 --> 00:01:53,000
So underscore underscore you can basically write it over here.

36
00:01:53,000 --> 00:01:58,000
So let's say if I go ahead and write underscore underscore GT this is for the greater than if you want

37
00:01:58,000 --> 00:02:00,000
to override that particular functionality.

38
00:02:00,000 --> 00:02:06,000
Now let me just show you over here and we will try to uh, overload some of the operations with respect

39
00:02:06,000 --> 00:02:08,000
to addition, subtraction and all.

40
00:02:08,000 --> 00:02:09,000
Okay.

41
00:02:09,000 --> 00:02:12,000
Uh, so first of all, I'm going to define my class, which is called as vector.

42
00:02:12,000 --> 00:02:18,000
So this is my, uh, sorry, I will just go ahead and define my class, which is nothing but vector.

43
00:02:18,000 --> 00:02:22,000
Now inside this vector I will be writing my init function.

44
00:02:22,000 --> 00:02:25,000
So here you basically have init function.

45
00:02:25,000 --> 00:02:29,000
And with respect to init function let's say I'm going to create two variables x comma y.

46
00:02:29,000 --> 00:02:32,000
Now if I go ahead and write a self dot x is equal to x.

47
00:02:32,000 --> 00:02:37,000
And uh here I'm going to write self dot y is equal to y.

48
00:02:37,000 --> 00:02:38,000
Okay.

49
00:02:38,000 --> 00:02:43,000
Now I'm going to just go ahead and add my add function over here.

50
00:02:43,000 --> 00:02:48,000
So let me just go go ahead and use this definition add underscore underscore.

51
00:02:48,000 --> 00:02:53,000
And here I have to probably give two parameters self comma other right.

52
00:02:53,000 --> 00:02:53,000
Right.

53
00:02:53,000 --> 00:02:56,000
So here what I'm doing I'm just giving one more additional parameter.

54
00:02:56,000 --> 00:03:03,000
Now what I'm actually going to do over here is that uh, in this addition operation I will just change

55
00:03:03,000 --> 00:03:04,000
change this entire operation itself.

56
00:03:04,000 --> 00:03:07,000
So I'll say, hey, go ahead and return.

57
00:03:07,000 --> 00:03:08,000
Okay.

58
00:03:08,000 --> 00:03:10,000
Vector class over here.

59
00:03:10,000 --> 00:03:14,000
And inside this vector class we'll give self dot x okay.

60
00:03:14,000 --> 00:03:17,000
Plus other dot x okay.

61
00:03:17,000 --> 00:03:20,000
I'm just putting some kind of operation.

62
00:03:20,000 --> 00:03:20,000
Okay.

63
00:03:20,000 --> 00:03:21,000
I'm doing some kind of operation over here.

64
00:03:21,000 --> 00:03:24,000
Self dot x okay.

65
00:03:24,000 --> 00:03:28,000
Not z x plus other dot x.

66
00:03:28,000 --> 00:03:34,000
And here what I'm actually going to do again I'll go ahead and write self dot y plus other dot y.

67
00:03:34,000 --> 00:03:37,000
That basically means what I'm actually going to do.

68
00:03:37,000 --> 00:03:42,000
This other will be an another vector itself which will have x and y coordinate.

69
00:03:42,000 --> 00:03:47,000
And I'm just summing it up with the x that I'm actually giving.

70
00:03:48,000 --> 00:03:51,000
And along with the y coordinate that I'm actually giving.

71
00:03:51,000 --> 00:03:51,000
Right.

72
00:03:51,000 --> 00:03:54,000
So I'm just going to sum it up with respect to this particular other.

73
00:03:54,000 --> 00:03:54,000
Right.

74
00:03:54,000 --> 00:04:01,000
Similarly what I will do I will do the same thing for uh, let's say subtraction for multiplication.

75
00:04:01,000 --> 00:04:04,000
And also here I will go ahead and define my another function.

76
00:04:04,000 --> 00:04:08,000
And here you can see I'm doing subtracting over here I'm doing uh subtracting over here.

77
00:04:08,000 --> 00:04:08,000
Right.

78
00:04:08,000 --> 00:04:10,000
Other will be another vector okay.

79
00:04:10,000 --> 00:04:14,000
We'll see to it once we properly initialize and call the function.

80
00:04:14,000 --> 00:04:18,000
Now along with this, uh, what I am actually going to do is that, uh, I'm also going to make sure

81
00:04:18,000 --> 00:04:22,000
to change my equal to functionality.

82
00:04:22,000 --> 00:04:22,000
Okay.

83
00:04:22,000 --> 00:04:25,000
I'm going to overload this particular operator itself.

84
00:04:25,000 --> 00:04:31,000
So here with respect to equal to I'm saying return self dot x double equal to other dot x and self dot

85
00:04:31,000 --> 00:04:33,000
y double equal to other dot y.

86
00:04:33,000 --> 00:04:37,000
It will be either true or false okay, based on the comparison that is basically happening.

87
00:04:37,000 --> 00:04:44,000
And finally, to represent this entire string, uh, we will be using underscore underscore r e p r.

88
00:04:44,000 --> 00:04:48,000
And here we am just displaying the vector with self dot x and self dot y.

89
00:04:48,000 --> 00:04:49,000
Okay.

90
00:04:49,000 --> 00:04:50,000
Just a simple operation.

91
00:04:51,000 --> 00:04:54,000
And just to show you that how I'm overriding each and everything.

92
00:04:54,000 --> 00:04:59,000
So finally we go ahead and create objects of the vector class.

93
00:04:59,000 --> 00:05:00,000
Okay.

94
00:05:00,000 --> 00:05:06,000
And here we are just going to write v one is equal to vector two comma three.

95
00:05:06,000 --> 00:05:06,000
Okay.

96
00:05:06,000 --> 00:05:10,000
And then I will be going to create my vector v two.

97
00:05:10,000 --> 00:05:13,000
And this will be nothing but four comma five okay.

98
00:05:14,000 --> 00:05:16,000
Now this is my v one and v two vector.

99
00:05:16,000 --> 00:05:17,000
Now let's see okay.

100
00:05:17,000 --> 00:05:23,000
What kind of operation will basically happen if I go ahead and write print v1 plus v2?

101
00:05:24,000 --> 00:05:25,000
We have already.

102
00:05:25,000 --> 00:05:29,000
You can see that I've already changed this plus underscore add operation.

103
00:05:29,000 --> 00:05:29,000
Right.

104
00:05:29,000 --> 00:05:33,000
So if I go ahead and print it so what is the output that I'm actually getting.

105
00:05:33,000 --> 00:05:35,000
See V1 is nothing.

106
00:05:35,000 --> 00:05:38,000
But so here you can see that I'm adding both this.

107
00:05:38,000 --> 00:05:42,000
Write two plus 463 plus five.

108
00:05:42,000 --> 00:05:42,000
Right.

109
00:05:42,000 --> 00:05:45,000
So that is what I'm printing it with respect to the addition operation.

110
00:05:45,000 --> 00:05:49,000
So that is what is basically happening self dot x other dot x.

111
00:05:49,000 --> 00:05:55,000
So in short I have modified this entire functionality for the addition of vectors.

112
00:05:55,000 --> 00:05:57,000
That is the main purpose over here okay.

113
00:05:57,000 --> 00:06:01,000
Please make sure that is the reason I had written initially the all the code.

114
00:06:01,000 --> 00:06:10,000
This is nothing but I'm doing mathematical operation for vectors, which is not available right now.

115
00:06:10,000 --> 00:06:12,000
So I have defined my own custom one here.

116
00:06:12,000 --> 00:06:18,000
What we have done here, this is one coordinate x comma y and this is the other coordinate x comma y.

117
00:06:18,000 --> 00:06:18,000
Right.

118
00:06:18,000 --> 00:06:24,000
And when we do add operation this is going to get considered V2 is going to get considered as the other

119
00:06:24,000 --> 00:06:25,000
option right.

120
00:06:25,000 --> 00:06:28,000
Then I'm going to take this as x and y coordinate.

121
00:06:28,000 --> 00:06:30,000
And this will be my other x and y coordinate.

122
00:06:30,000 --> 00:06:32,000
And this is what we are getting over here.

123
00:06:32,000 --> 00:06:35,000
And we will be able to add those vectors.

124
00:06:35,000 --> 00:06:35,000
Right.

125
00:06:35,000 --> 00:06:39,000
So next I will go ahead and write v one minus v two.

126
00:06:39,000 --> 00:06:44,000
Similarly print v one multiplied by three okay.

127
00:06:44,000 --> 00:06:47,000
So if I go ahead and execute it first of all v one minus v two.

128
00:06:47,000 --> 00:06:51,000
What it is going to do self dot x multiplied by other.

129
00:06:51,000 --> 00:06:54,000
So sorry this is multiplication.

130
00:06:54,000 --> 00:06:56,000
So self dot x minus other dot x.

131
00:06:56,000 --> 00:07:01,000
So we are just going to subtract it two minus four is two and three minus five is minus two.

132
00:07:01,000 --> 00:07:04,000
Sorry two minus four is minus two and three minus five is minus two.

133
00:07:04,000 --> 00:07:09,000
And here you will be able to see that with respect to multiplication operation I am just multiplying

134
00:07:09,000 --> 00:07:11,000
with this particular number v one multiplied by three.

135
00:07:11,000 --> 00:07:13,000
So I have x and y coordinate.

136
00:07:13,000 --> 00:07:18,000
So x multiplied by other number over here three and y multiplied by three.

137
00:07:18,000 --> 00:07:20,000
It is nothing but six comma nine right?

138
00:07:20,000 --> 00:07:26,000
So this is how you can entirely overload all the operators based on your assumption.

139
00:07:26,000 --> 00:07:32,000
You can take up different different operators and definitely make sure that you try to, uh, you know,

140
00:07:32,000 --> 00:07:36,000
convert them based on your vectors or based on your requirements that you like.

141
00:07:36,000 --> 00:07:42,000
I will also give you one more, uh, example, which you can do it as your assignment.

142
00:07:42,000 --> 00:07:46,000
That is nothing but overloading operators for complex numbers.

143
00:07:46,000 --> 00:07:49,000
You can just go ahead and explore this particular code.

144
00:07:49,000 --> 00:07:52,000
Now it's time that you have to give some amount of time.

145
00:07:52,000 --> 00:07:56,000
And here we have also used true div and see what all things we are basically doing.

146
00:07:56,000 --> 00:07:59,000
If I talk about complex number, there will be an imaginary number.

147
00:07:59,000 --> 00:08:01,000
So there will be a real number.

148
00:08:01,000 --> 00:08:02,000
It will be an imaginary number.

149
00:08:02,000 --> 00:08:04,000
And based on that we have written this code.

150
00:08:04,000 --> 00:08:09,000
So Joe, just go ahead and explore this and let me know whether you are able to understand or not.

151
00:08:10,000 --> 00:08:16,000
So finally, I would like to add some conclusion saying that, uh, magic methods and operator overloading

152
00:08:16,000 --> 00:08:22,000
are some amazing, powerful feature, uh, in Python, allowing you to completely customize the operator

153
00:08:22,000 --> 00:08:26,000
functionalities itself based on your application development that you really want.

154
00:08:26,000 --> 00:08:27,000
Right?

155
00:08:27,000 --> 00:08:28,000
So yes, this was it from my side.

156
00:08:28,000 --> 00:08:30,000
I hope you liked this particular video.

157
00:08:30,000 --> 00:08:31,000
I will see you in the next video.

158
00:08:31,000 --> 00:08:31,000
Thank you.

159
00:08:31,000 --> 00:08:31,000
Take.

