You've waited a long time for this because I've avoided talking about these Dunder Magic methods for
a long time.
In this video, we're finally going to uncover what they do.
Now we're able to see what our instance has as methods using the rder method.
And we told you that these were inherited from our base object class.
Now this underscores our special methods.
Remember how we have things like length, for example, that allows us to tell the length of an array.
These are actually implemented using these dunder methods.
They allow us to use Python specific functions on objects created through our class.
So let me show you what I mean.
We're going to create a new class and this is going to be a toy class.
Now in here, we'll define a toy and initialize it with self.
Then we'll say that it has color and h.
Now we'll say color equals to color.
So these will be our attributes.
And H.
Awesome.
There's our class.
Now, let's say we want to create an action figure.
This action figure will be instantiated with toy, let's say, color of red and the ages.
It's brand new, so it's zero.
Now if I use something like, let's say se tr that comes with this object, if I do str or action figure.
Dot underscore underscore se tt ah like so.
And we print this.
We see that we get this.
It's the exact same thing as us doing.
SDR Action Figure.
Like this.
Look at that.
These double underscore dunder methods are special methods that Python recognizes.
For example, the Dunder STR.
This special method allows us to use action figure like this using the str built in function.
As a matter of fact, if you go to the Python documentation under the special method names section.
You'll see that we can do basic customizations of these dunder methods.
And it's a long list of all these dunder methods that we can use.
Now.
Don't go and read every single one of them.
There are a lot and you're not going to use them that often.
There's only specific few cases that you're going to use them, and the idea is to know how to modify
them and how they work underneath the hood.
So.
Let's go over a few examples.
For example, let's modify what the SDR does.
I'm going to define my own.
SDR here.
I'm going to call self.
And then in here, I'm going to return something different.
I'm going to return, let's say, an SE string.
That is, let's say, self-taught color.
If I run this.
I get red.
Remember how I said not to modify Dunder methods?
Well, sometimes you're allowed to.
Maybe there are cases, special cases, where you want your class to behave a certain way.
Just like dictionaries, lists, tuples, and all our objects in Python can behave in certain ways.
How lists were accessed with an index number and a dictionary was accessed with a key.
Those are all implemented using these standard methods.
And you see here that because we modified this, the SDR now returns red when being called on action
figure.
It doesn't change things other than within action figure.
If I run this, you'll see that it still runs action figure like this.
Because this is a string.
The SDR is only modified when we use it on this specific object.
Like so.
What other neat things can we do?
Well, we can change the meaning of the length as well.
So length I can say self.
And in here I can just return.
Now if I print.
Length of action figure.
And I click Run.
I get five?
Very cool.
By the way, if this underline is getting a little annoying, which frankly for me it's getting a little
bit annoying, I'm going to click on settings here and just remove the code intelligence so it won't
tell me when my code is wrong, but hopefully we can just debug it ourselves.
All right, so there we go.
We've just changed the length.
Dunder to return five.
Let's keep experimenting.
We can also do something like underscore, underscore, delete and delete.
Well, lets us actually use the delete keyword which we actually haven't seen before.
Delete.
We can just say return.
Deleted.
You have to go like this and let's just do print here.
Print deleted if I click run.
I see that I returned deleted.
Now the delete keyword I haven't really taught you because it's not something that you'll see a lot
of.
Usually this can cause some funny bugs in our programs that I like to personally avoid, but the the
delete keyword deletes some sort of variable that we might have had in our program.
All right, what else is there?
Let's change this.
To use something called call.
And coal is an interesting one.
Let's say that with coal, we're going to return and let's say yes.
Question mark.
The neat thing about Carl is that now it gives our toy class some superpower.
We can say action figure and then say, well, with call, we actually can now call the actual object
with these brackets.
So using call, I have this special way to call a function which, by the way, underneath the hood,
the way we're able to call functions is using this dunder call.
And if I click Run.
I get?
Yes.
How cool is that?
All right.
One last one, because I think you're starting to get the idea of this.
Like I said, these are all available in here.
Usually you don't want to overwrite them, but you just want to know that you have the power to do so
if you choose.
The next one is called get item.
So let's do define get item.
Again, we're going to use self as the first parameter and then let's say I for index and in here we'll
return self dot and let's add an attribute before we add that.
I'm going to say self dot my dict for my dictionary and then add name.
Equals to yo yo.
And let's do has pets equal to false?
Now using this, I can say my dicked.
And return the item based on the index that we give it.
So let me show you what I mean.
If I now do print.
Action figure.
Dot.
And in here, instead of doing the dot, we do the square brackets just like we would access perhaps
getting a dictionary item.
We can now using the get item, say what properties do we have in name?
So let's say name and if I print this oop.
Forgot to add a.
Comma in here.
Let's try that again.
And if I scroll down you can see I'm really good at reading errors.
Namer Name action figure is not defined because it's action figure not fuga.
That's better.
Look at that.
My name.
Yo Yo was being able to access using this bracket notation using the get item.
Dunder.
Very, very cool.
So these special magic methods are really interesting because it allows us to do some well, some custom
modifying of our classes.
And you can also understand how in Python are built in default types had access and abilities to have
all these special syntax.
We now have that power as well.
Let's take a break and do a bit of an exercise.