Hello guys.
So we are going to continue a discussion with respect to Python.
And in this video we are going to discuss about magic methods.
Now till now we have discussed so many different topics regarding classes regarding oops, you know,
we have discussed about function overriding, uh method overriding.
We have discussed about encapsulation, abstraction, so many things we have discussed and where some
or the other way.
You have definitely seen this magic methods.
So what exactly it is?
Uh, we'll go in depth, we'll try to understand it and we'll see a coding examples with respect to
this.
So magic methods in Python is also known as dunder methods, which is nothing but double underscore
methods.
Right.
They are special methods that start and end with double underscore.
This methods enable you to define the behavior of the object for built in operations such as arithmetic,
operation, comparison, and more.
Some of the examples, if, uh, I probably, uh, go ahead and make you understand some of the examples
are something like this.
See, I will just go ahead and write like this.
I think you should be able to see it.
It is not showing double underscore but I don't know why.
So let's see okay I'll just make this changes and let's see whether we are able to see double underscore.
No it is not double underscore.
So let me just go ahead and take this entirely over here okay.
And I will take a code okay.
I'll use a triple quote code.
Okay.
If I want.
First of all, I will go ahead and select a kernel so that you will be able to understand what I'm actually
talking about.
So some of the examples with respect to this kind of operators are nothing but like this.
Right.
So magic methods are nothing but like underscore underscore init underscore underscore.
This basically means you initialize a new instance of a class, right.
Basically the constructor.
You also have some operators or some methods like underscore underscore HDR, underscore.
So it returns a string representation of an object.
Right.
And similarly some of the examples are over here.
And if you really want to see this you will also be able to see this.
Because once I probably create a class, let's say I'm going to create a class person.
And inside this I'm just going to write pass okay.
And now if I go ahead and create an object for this particular person.
So here you'll be able to see this class okay.
Now if I go ahead and write directory of person right.
You'll be able to see all this.
Right.
So these are nothing.
But these are called as magic methods.
Right.
And we can use all these magic methods.
And this magic methods are by default used to uh here here it is very clearly written that why it is
used to it is used to define the behavior of an object for built in operations such as arithmetic comparison
and many more.
Let's say I want to use underscore underscore str.
This will just return a string representation of an object right.
So let's say that I want to just go ahead and print the person object.
So by default you'll be seeing that hey I'm getting this message.
And this message is basically getting displayed from this particular function.
That is nothing but underscore underscore str underscore underscore.
And there are something like you can also override this specific method.
And that is what I'm actually going to show you over here Okay.
Now let's go ahead and explore some of the basic, uh, magic methods.
And then we will try to, uh, override them also.
So let's say that I'm writing definition underscore underscore init underscore underscore.
And here I'm going to write self self comma name comma age okay.
Now once I write this I'm just going to write self dot name is equal to name okay.
And along with this I'm just going to say hey uh self dot age is equal to age okay.
So we have defined this over here and uh, I think uh okay.
I did not write the class.
What it got deleted I guess.
So class class person okay.
Person.
And then let me just go ahead and press this DF.
And here I'm just going to press enter, make sure that you have this indentation.
If indentation is not there then it will be a problem okay.
Now I have defined this particular person class okay.
Now let's say that I go ahead and define my, uh, person object.
And let me just go ahead and write the name over here like Krish comma, comma, uh, age is 34.
And I'll just go ahead and print person.
Okay.
Now when I'm printing person by default, I will be getting this information.
Okay.
This particular object is created at this particular memory location.
Let's say that I want to override this kind of message.
So I have to override this underscore underscore str method.
So what I will do now I will just copy the same thing over here.
I'll paste it over here.
And now I will just go ahead and define another function another magic function over here.
And this will be nothing but str underscore underscore here also we'll give self okay.
And now along with this what I'm actually going to do I'm going to return some kind of message.
Now I'm overriding overriding the default functionality of this magic method.
So here I'm going to basically write return F.
And let me just go ahead and write.
This is nothing but my self dot name.
I'll just try to display the self dot name.
And along with this self dot name I will just go ahead and write comma self.
Self.
Self dot age.
Years old.
Okay.
And I will just go ahead and display this years old.
Old.
And let me just close this brackets over here.
Okay.
So here you can see that now I have changed the functionality of this magic method that is underscore
underscore str which was responsible in displaying the information regarding the object.
Right now, if I go ahead and execute it now, you'll be able to see that this message is getting executed.
Before, the default message was something like this.
Right.
And obviously, you know over here y underscore underscore HDR is used.
It is used to return a string representation of our object.
And now when I'm actually using this and you can see that I have overridden the functionality and I
have written my own way of printing the message.
And that is what is basically getting displayed, right?
Similarly, uh, you can also, um, you know, use different, different functionalities.
Let's say one more, uh, functionality is that is present is nothing but underscore underscore r e
p r.
And this is basically for I've written it over here if you go up.
Right.
Why it is specifically used underscore underscore.
It returns an official string representation of an object.
Right.
So if I go ahead and take this and now I want to return and write my own string, I can go ahead and
write.
I can say person, person.
Let's say I will define something like this name is equal to self dot name okay self dot name.
And here I will go ahead and write age is equal to self dot age Okay, something like this I want to
display in my own way.
Okay.
Now if I want to call this function also I can also call it.
So I will go ahead and write print um r e p r.
And with respect to that I will just go ahead and call this particular person over here.
If I execute it here you can see person name is equal to crush.
Age is equal to 34.
So this is how in my own custom string representation I have displayed it.
So magic methods, they are some very important functionalities which will be doing some default function
or default implementation for objects.
If you really want to override it, just write that particular magic method in this particular way and
override it based on your comfort and your application.
So I hope you like this particular video.
Uh, this was about, uh, the magic methods.
And, uh, I've also shown you that how you can override to change the behavior of the object.
And, uh, I've just taken some of the examples.
You can take any number of examples and all the magic methods you'll be able to see over here.
And further, you can do Google uh, and probably find out a lot of different operations like TT, uh,
doc and all.
So in the next session also I'll be discussing about operator overloading.
I will be talking more about this magic method over there.
Right.
So yes, uh, this was it.
I will see you all in the next video.
Thank you.