1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue a discussion with respect to Python.

3
00:00:03,000 --> 00:00:07,000
And in this video we are going to discuss about magic methods.

4
00:00:07,000 --> 00:00:12,000
Now till now we have discussed so many different topics regarding classes regarding oops, you know,

5
00:00:12,000 --> 00:00:16,000
we have discussed about function overriding, uh method overriding.

6
00:00:16,000 --> 00:00:22,000
We have discussed about encapsulation, abstraction, so many things we have discussed and where some

7
00:00:22,000 --> 00:00:22,000
or the other way.

8
00:00:22,000 --> 00:00:24,000
You have definitely seen this magic methods.

9
00:00:24,000 --> 00:00:26,000
So what exactly it is?

10
00:00:26,000 --> 00:00:31,000
Uh, we'll go in depth, we'll try to understand it and we'll see a coding examples with respect to

11
00:00:31,000 --> 00:00:31,000
this.

12
00:00:31,000 --> 00:00:37,000
So magic methods in Python is also known as dunder methods, which is nothing but double underscore

13
00:00:37,000 --> 00:00:37,000
methods.

14
00:00:37,000 --> 00:00:38,000
Right.

15
00:00:38,000 --> 00:00:41,000
They are special methods that start and end with double underscore.

16
00:00:41,000 --> 00:00:46,000
This methods enable you to define the behavior of the object for built in operations such as arithmetic,

17
00:00:46,000 --> 00:00:47,000
operation, comparison, and more.

18
00:00:47,000 --> 00:00:54,000
Some of the examples, if, uh, I probably, uh, go ahead and make you understand some of the examples

19
00:00:54,000 --> 00:00:55,000
are something like this.

20
00:00:55,000 --> 00:00:57,000
See, I will just go ahead and write like this.

21
00:00:57,000 --> 00:00:59,000
I think you should be able to see it.

22
00:00:59,000 --> 00:01:03,000
It is not showing double underscore but I don't know why.

23
00:01:03,000 --> 00:01:08,000
So let's see okay I'll just make this changes and let's see whether we are able to see double underscore.

24
00:01:08,000 --> 00:01:10,000
No it is not double underscore.

25
00:01:10,000 --> 00:01:14,000
So let me just go ahead and take this entirely over here okay.

26
00:01:14,000 --> 00:01:17,000
And I will take a code okay.

27
00:01:17,000 --> 00:01:19,000
I'll use a triple quote code.

28
00:01:19,000 --> 00:01:20,000
Okay.

29
00:01:20,000 --> 00:01:21,000
If I want.

30
00:01:21,000 --> 00:01:26,000
First of all, I will go ahead and select a kernel so that you will be able to understand what I'm actually

31
00:01:26,000 --> 00:01:27,000
talking about.

32
00:01:27,000 --> 00:01:32,000
So some of the examples with respect to this kind of operators are nothing but like this.

33
00:01:32,000 --> 00:01:33,000
Right.

34
00:01:33,000 --> 00:01:37,000
So magic methods are nothing but like underscore underscore init underscore underscore.

35
00:01:37,000 --> 00:01:40,000
This basically means you initialize a new instance of a class, right.

36
00:01:40,000 --> 00:01:42,000
Basically the constructor.

37
00:01:42,000 --> 00:01:46,000
You also have some operators or some methods like underscore underscore HDR, underscore.

38
00:01:46,000 --> 00:01:48,000
So it returns a string representation of an object.

39
00:01:48,000 --> 00:01:49,000
Right.

40
00:01:49,000 --> 00:01:51,000
And similarly some of the examples are over here.

41
00:01:51,000 --> 00:01:55,000
And if you really want to see this you will also be able to see this.

42
00:01:55,000 --> 00:01:59,000
Because once I probably create a class, let's say I'm going to create a class person.

43
00:01:59,000 --> 00:02:02,000
And inside this I'm just going to write pass okay.

44
00:02:02,000 --> 00:02:05,000
And now if I go ahead and create an object for this particular person.

45
00:02:05,000 --> 00:02:08,000
So here you'll be able to see this class okay.

46
00:02:08,000 --> 00:02:11,000
Now if I go ahead and write directory of person right.

47
00:02:11,000 --> 00:02:12,000
You'll be able to see all this.

48
00:02:12,000 --> 00:02:13,000
Right.

49
00:02:13,000 --> 00:02:14,000
So these are nothing.

50
00:02:14,000 --> 00:02:16,000
But these are called as magic methods.

51
00:02:16,000 --> 00:02:16,000
Right.

52
00:02:16,000 --> 00:02:19,000
And we can use all these magic methods.

53
00:02:19,000 --> 00:02:25,000
And this magic methods are by default used to uh here here it is very clearly written that why it is

54
00:02:25,000 --> 00:02:30,000
used to it is used to define the behavior of an object for built in operations such as arithmetic comparison

55
00:02:30,000 --> 00:02:31,000
and many more.

56
00:02:31,000 --> 00:02:33,000
Let's say I want to use underscore underscore str.

57
00:02:33,000 --> 00:02:36,000
This will just return a string representation of an object right.

58
00:02:36,000 --> 00:02:42,000
So let's say that I want to just go ahead and print the person object.

59
00:02:42,000 --> 00:02:45,000
So by default you'll be seeing that hey I'm getting this message.

60
00:02:45,000 --> 00:02:50,000
And this message is basically getting displayed from this particular function.

61
00:02:50,000 --> 00:02:53,000
That is nothing but underscore underscore str underscore underscore.

62
00:02:53,000 --> 00:02:58,000
And there are something like you can also override this specific method.

63
00:02:58,000 --> 00:03:01,000
And that is what I'm actually going to show you over here Okay.

64
00:03:01,000 --> 00:03:06,000
Now let's go ahead and explore some of the basic, uh, magic methods.

65
00:03:06,000 --> 00:03:08,000
And then we will try to, uh, override them also.

66
00:03:08,000 --> 00:03:12,000
So let's say that I'm writing definition underscore underscore init underscore underscore.

67
00:03:12,000 --> 00:03:19,000
And here I'm going to write self self comma name comma age okay.

68
00:03:19,000 --> 00:03:23,000
Now once I write this I'm just going to write self dot name is equal to name okay.

69
00:03:23,000 --> 00:03:30,000
And along with this I'm just going to say hey uh self dot age is equal to age okay.

70
00:03:30,000 --> 00:03:33,000
So we have defined this over here and uh, I think uh okay.

71
00:03:33,000 --> 00:03:35,000
I did not write the class.

72
00:03:35,000 --> 00:03:37,000
What it got deleted I guess.

73
00:03:37,000 --> 00:03:41,000
So class class person okay.

74
00:03:42,000 --> 00:03:42,000
Person.

75
00:03:43,000 --> 00:03:47,000
And then let me just go ahead and press this DF.

76
00:03:47,000 --> 00:03:52,000
And here I'm just going to press enter, make sure that you have this indentation.

77
00:03:52,000 --> 00:03:55,000
If indentation is not there then it will be a problem okay.

78
00:03:56,000 --> 00:03:58,000
Now I have defined this particular person class okay.

79
00:03:58,000 --> 00:04:02,000
Now let's say that I go ahead and define my, uh, person object.

80
00:04:02,000 --> 00:04:11,000
And let me just go ahead and write the name over here like Krish comma, comma, uh, age is 34.

81
00:04:11,000 --> 00:04:14,000
And I'll just go ahead and print person.

82
00:04:14,000 --> 00:04:14,000
Okay.

83
00:04:14,000 --> 00:04:19,000
Now when I'm printing person by default, I will be getting this information.

84
00:04:19,000 --> 00:04:19,000
Okay.

85
00:04:19,000 --> 00:04:22,000
This particular object is created at this particular memory location.

86
00:04:22,000 --> 00:04:25,000
Let's say that I want to override this kind of message.

87
00:04:25,000 --> 00:04:28,000
So I have to override this underscore underscore str method.

88
00:04:28,000 --> 00:04:31,000
So what I will do now I will just copy the same thing over here.

89
00:04:31,000 --> 00:04:32,000
I'll paste it over here.

90
00:04:32,000 --> 00:04:37,000
And now I will just go ahead and define another function another magic function over here.

91
00:04:37,000 --> 00:04:42,000
And this will be nothing but str underscore underscore here also we'll give self okay.

92
00:04:42,000 --> 00:04:46,000
And now along with this what I'm actually going to do I'm going to return some kind of message.

93
00:04:46,000 --> 00:04:51,000
Now I'm overriding overriding the default functionality of this magic method.

94
00:04:51,000 --> 00:04:54,000
So here I'm going to basically write return F.

95
00:04:54,000 --> 00:04:55,000
And let me just go ahead and write.

96
00:04:55,000 --> 00:04:57,000
This is nothing but my self dot name.

97
00:04:57,000 --> 00:05:00,000
I'll just try to display the self dot name.

98
00:05:00,000 --> 00:05:06,000
And along with this self dot name I will just go ahead and write comma self.

99
00:05:06,000 --> 00:05:06,000
Self.

100
00:05:06,000 --> 00:05:09,000
Self dot age.

101
00:05:09,000 --> 00:05:10,000
Years old.

102
00:05:11,000 --> 00:05:11,000
Okay.

103
00:05:11,000 --> 00:05:15,000
And I will just go ahead and display this years old.

104
00:05:16,000 --> 00:05:17,000
Old.

105
00:05:17,000 --> 00:05:21,000
And let me just close this brackets over here.

106
00:05:21,000 --> 00:05:21,000
Okay.

107
00:05:21,000 --> 00:05:28,000
So here you can see that now I have changed the functionality of this magic method that is underscore

108
00:05:28,000 --> 00:05:32,000
underscore str which was responsible in displaying the information regarding the object.

109
00:05:32,000 --> 00:05:38,000
Right now, if I go ahead and execute it now, you'll be able to see that this message is getting executed.

110
00:05:38,000 --> 00:05:41,000
Before, the default message was something like this.

111
00:05:41,000 --> 00:05:41,000
Right.

112
00:05:42,000 --> 00:05:45,000
And obviously, you know over here y underscore underscore HDR is used.

113
00:05:45,000 --> 00:05:48,000
It is used to return a string representation of our object.

114
00:05:48,000 --> 00:05:53,000
And now when I'm actually using this and you can see that I have overridden the functionality and I

115
00:05:53,000 --> 00:05:56,000
have written my own way of printing the message.

116
00:05:56,000 --> 00:05:59,000
And that is what is basically getting displayed, right?

117
00:05:59,000 --> 00:06:05,000
Similarly, uh, you can also, um, you know, use different, different functionalities.

118
00:06:05,000 --> 00:06:10,000
Let's say one more, uh, functionality is that is present is nothing but underscore underscore r e

119
00:06:10,000 --> 00:06:11,000
p r.

120
00:06:11,000 --> 00:06:15,000
And this is basically for I've written it over here if you go up.

121
00:06:15,000 --> 00:06:16,000
Right.

122
00:06:16,000 --> 00:06:18,000
Why it is specifically used underscore underscore.

123
00:06:18,000 --> 00:06:21,000
It returns an official string representation of an object.

124
00:06:21,000 --> 00:06:21,000
Right.

125
00:06:21,000 --> 00:06:30,000
So if I go ahead and take this and now I want to return and write my own string, I can go ahead and

126
00:06:30,000 --> 00:06:30,000
write.

127
00:06:30,000 --> 00:06:33,000
I can say person, person.

128
00:06:33,000 --> 00:06:40,000
Let's say I will define something like this name is equal to self dot name okay self dot name.

129
00:06:40,000 --> 00:06:48,000
And here I will go ahead and write age is equal to self dot age Okay, something like this I want to

130
00:06:48,000 --> 00:06:50,000
display in my own way.

131
00:06:50,000 --> 00:06:50,000
Okay.

132
00:06:50,000 --> 00:06:53,000
Now if I want to call this function also I can also call it.

133
00:06:53,000 --> 00:06:57,000
So I will go ahead and write print um r e p r.

134
00:06:57,000 --> 00:07:02,000
And with respect to that I will just go ahead and call this particular person over here.

135
00:07:02,000 --> 00:07:06,000
If I execute it here you can see person name is equal to crush.

136
00:07:06,000 --> 00:07:07,000
Age is equal to 34.

137
00:07:07,000 --> 00:07:11,000
So this is how in my own custom string representation I have displayed it.

138
00:07:11,000 --> 00:07:17,000
So magic methods, they are some very important functionalities which will be doing some default function

139
00:07:17,000 --> 00:07:20,000
or default implementation for objects.

140
00:07:20,000 --> 00:07:24,000
If you really want to override it, just write that particular magic method in this particular way and

141
00:07:24,000 --> 00:07:27,000
override it based on your comfort and your application.

142
00:07:27,000 --> 00:07:29,000
So I hope you like this particular video.

143
00:07:29,000 --> 00:07:33,000
Uh, this was about, uh, the magic methods.

144
00:07:33,000 --> 00:07:38,000
And, uh, I've also shown you that how you can override to change the behavior of the object.

145
00:07:38,000 --> 00:07:41,000
And, uh, I've just taken some of the examples.

146
00:07:41,000 --> 00:07:45,000
You can take any number of examples and all the magic methods you'll be able to see over here.

147
00:07:45,000 --> 00:07:51,000
And further, you can do Google uh, and probably find out a lot of different operations like TT, uh,

148
00:07:51,000 --> 00:07:52,000
doc and all.

149
00:07:52,000 --> 00:07:55,000
So in the next session also I'll be discussing about operator overloading.

150
00:07:55,000 --> 00:07:59,000
I will be talking more about this magic method over there.

151
00:07:59,000 --> 00:07:59,000
Right.

152
00:07:59,000 --> 00:08:01,000
So yes, uh, this was it.

153
00:08:01,000 --> 00:08:03,000
I will see you all in the next video.

154
00:08:03,000 --> 00:08:03,000
Thank you.

