Let's take a look at a "dunder" method.
Now, we've seen this already with a class, any class called
'Test', and we said 'def __init__'
This '__' is called a 'double underscore',
or in Python terms, we call it a 'dunder'.
And this '__init__' method here is called a 'dunder' method.
And Python actually gives us a bunch of these to work with
right out of the box.
And in this lesson, we're going to cover three of those.
So let's go ahead and create an example here where we have
a book, 'class Book:', and 'def __init__(self,', and the book 'title', and
total 'pages', I guess,
and maybe the 'content' of the book
I guess. This is just an example.
So we do 'self.title = title',
'self.pages =,
let's cast this as an integer, 'int(pages)', and 'self.content'
is going to be whatever the 'content' is.
So let's create a new book in here.
'harry_potter', a book you may or may not have heard of, this is
going to be a new 'Book()', 'title = "Harry Potter and the Prisoner
of Azkaban', or something. I don't know,
is it 250 pages, something like that, and then you know some 'content' in here.
And that could actually be the book contents if we wanted to. Okay.
So now we have this new class instantiated.
And if we just type 'harry_potter', we see that it's a 'Book'
at some memory location.
And if we do 'type(harry_potter)', it just says it's a 'Book'.
So this one is okay.
But what if you wanted a better name for this?
What if you didn't just want it to be named whatever the
memory location is? There is a nicer way to deal with it.
And you actually do use 'class names' a lot.
And so one way we do that, or really the way we do that
is with a 'dunder' method called, string, '__str__',
and so that's going to take 'self',
and whatever we return in here is what this class is
going to be called. So,
"Test Book".
Okay. So I'm just going to rerun these cells, and we're going to
see that 'harry_potter' is still pointing to some memory
location, and that's okay.
But what we want to do is print this out.
If we print this out, it says "Test Book".
So let's go ahead and actually delete this, because I got
one step ahead of us here.
Rerun this. Again,
when we print out 'harry_potter' without the 'dunder', '__str__'
in there, it just points to a location in our memory.
But if we put '__str__', because we have
this in here, that is the string representation of this 'Book'.
Now, what we can actually do is change this to be 'self.title'.
Rerun that, that, and that,
and this is called "Harry Potter".
Now we can start another book.
I'm going to get rid of 'type' here.
And let's call it Lord of the Rings, 'lotr = Book(
"Lord of the Rings". I don't know,
it's a 900 page book? Sometimes feels like it.
And it's got some 'content' in there.
We can do 'print()', Lord of the Rings, 'lotr', and we now know what this class
is, it's called "Lord of the Rings".
We know it's a 'Book', because if we do 'type(lotr)',
[no audio]
it is a 'Book'.
Now let's go ahead and add a little bit more to this.
What if we wanted to get the total length of the book,
like how many pages there?
We could actually just use 'Book.pages',
but what if we wanted to get the total length, the total number
of words, for instance? So we could do 'def __len__(self):',
and just as an example, let's return 150, just for funsies.
We'll rerun that, that,
and in here we can run 'len(harry_potter)', and it gives us 150.
So now we can actually perform some logic in here.
We can say 'words = self.content.split(' ')', and then
return the total 'len(words)'.
Now we actually need to have some words in here,
and so I just filled this with 'Lorem ipsum',
it's 25 words of 'Lorem ipsum'.
No idea what it means.
I hope it's not offensive.
And when I rerun 'len(harry_potter), it's going to get the total
content, which is this one here.
It's going to split it by every single space, and then it's
going to return the total number of words.
And there we go.
We have 25 words of 'Lorem Ipsum' in there. That's exactly what I
put into it, 25 words of 'Lorem ipsum'.
So these are just three 'dunder' methods that come with
pretty much every class, and you can overwrite them to be whatever
you want, and let's see what other methods there are.
So let's add a new cell here.
And in here, let's do 'harry_potter.', hit 'Tab',
but then if you write an '_', you can actually see that
there's 'class',
'del', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', all sorts
of stuff in here, and these are all magic methods.
These are 'dunder' methods that come with Python.
A few of these you're going to see a lot more than other ones though.
And some of these you probably won't ever see.
So 'class' you will probably see in a framework like Django,
'del', or 'delattr',
you'll probably see that in data science stuff where you need
to free up memory pretty fast.
'dict', is actually a pretty cool one.
I use this one for debugging a lot, and this one just turns
your whole class into a dictionary.
So you've got 'title', 'pages', and 'content' in there.
That's pretty cool. That's a good one.
We also have 'dir', which will show you all sorts of stuff,
all the attributes, as well as all of the methods that are available in here.
So that's a good one to sort of discover what a class has
if you have not written the class yourself. 'init', yep,
we've used that one a lot. 'init_subclass' is a good one.
'len' we've looked at already. 'repr',
you might end up seeing that one as well.
That's a good one to set, but
that's a, that's a best practice that a lot of people, as far as
I can tell, don't tend to follow but it's a good one.
And 'str',
so that's the string representation.
That's the name of the current class.
And, yeah, there you go. So 'dunder' methods,
there are a lot of them, a lot of them you probably won't
use on a day to day basis.
Some of them you will.
I use 'init' and 'str' almost every single day.
I've used 'repr' quite a bit, and 'len' is a good one,
but as a web developer, I use Django,
and I don't really need to use that one too much, because it
usually comes with classes.
But should you ever need to do that, you can do that.
So if you ever create a digital bookstore and you want to
use the number of words as the total book length, you can do that, too.
You can get the total number of words and the average number
of words that a human reads per minute,
and then you could say that a "Book's length is, in minutes,
180 minutes", something like that.
Anyways, there are uses for it, but these are three pretty
popular ones that I thought I would like to just sort of
introduce you to and get you a little more familiar with.