Welcome back, everyone, to this last lecture on our discussion of object oriented programming.
Well, we're going to be discussing what are known as special methods, also sometimes called thunder
methods, because of the fact that they used to double underscores on each side of the method call.
Let's explore this further.
Are some examples.
OK, here I am inside a Python file.
And remember that Python itself has special, built in predefined functions.
And a really simple example of that is the fact that you can print out objects.
So print is a built in function.
There's also the ability to check in the length of an object.
That's also a special built in function.
And you'll notice that Python objects can then interact with these built in functions.
For example, if I create a list, my list one two three, I could say print my list.
I could also do something like print the length of my list.
And if I save those changes and run this file notice, I get back the list and then the fact that there's
an integer amount of items in it, like three.
Now the question arises, if we have created our own user defined classes or python objects, how would
Python know what to actually print when you print a user defined object?
How does Python know what the length of a user defined object is?
Let's show a really simple example by creating our own class for an object we call a book.
Maybe later on, we're developing a bookstore web application, and I've created my own custom object
called Book in order to help organize and define what a book is.
So upon instantiating a book, I call the init method here I pass in self, and let's imagine a book
has a title.
Has an author and also has a number of pages associated with it.
And so these are attributes that when we maybe register a new book into our little Amazon online store,
then I register a title author and pages, maybe later on or else register a price or something like
that.
So we're going to say self-built title is equal to the title passed in self.
The author is equal to the author Peston and then self-taught pages is equal to the pages passed then.
And what I'm going to do is create an instance of my book and let's create a new book.
This book's title could be Python Rocks.
The author could be me, Josie.
And let's say the pages is one hundred and twenty pages long.
Now the question is what happens if I ask Python to print out my book?
We just saw it work with my list.
So let's see what happens if I say prints.
My book, so I say this and that one, I run this, I get back this kind of ugly default print, which
is, hey, inside your main script, which is example that pie.
Here you've created a book object at this location in memory.
This is not super useful for someone who's trying to maybe debug code or display something to a client's
output, or they're just trying to print out my book.
So clearly there's issues here on what Python can actually do for us just by default.
And then if I were to do something like print out the length of my book, we definitely haven't told
Python what the length of a book is.
The python by itself is not going to understand that pages is probably the length.
In fact, it's just going to give us an error here if we try to run this.
It says, Hey, sorry, but the object of type book has no length because we haven't actually added
in the special methods associated with these built in functions.
You can think of special methods or thunder methods as essentially methods that are linked to built
in functions or special calls within Python.
So let's actually now show how we can create our own custom string representation of an object in order
for it to be printed out, as well as our own custom length of an object for it to be printed out.
We're going to start off with the simpler one, which is printing out an object.
So by default, Python can print out an object by reporting it's an object at some location in memory.
Typically, however, what we're going to want to do is have our own prints or string representation.
So this method that's associated with print is underscore, underscore note the two sets of underscores
and you can see there's actually a bunch of different methods here related to that.
And then it's S.T.A.R. underscore underscore and we pass in self colon.
And what this method does is whenever Python needs some sort of string representation of your object,
whether it's using something like print formatting, like if string literals or is trying to print the
object itself, it looks up.
Does this actual object have a string representation defined by the user that it wants me to display?
And in this case, remember, you're always going to return here.
Don't get confused by the fact that it's connected to print.
You do not use prints inside SDR methods.
Instead, you're going to return the string representation you want for your book.
And this is where you actually have a lot of flexibility.
But maybe I just want to say the title and author for my book, so I'm going to return and f string
literal here.
That just says the title like self-taught.
Title Curly Braces.
Written by and then the author.
Self-taught author.
And now it's going to happen is every time Python needs a string representation of the object, it's
going to ask for this method to be executed and return whatever that method happens to return.
And then that's what it's going to actually print out when it prints out my book.
So if you save these changes and now run this file instead of seeing this kind of ugly little thing
that says, Hey, book object in this point of memory, I now get to see the title written by Jose.
And again, you have a lot of flexibility over how you should report this or what you want to actually
print it out.
I just want to point out to fact that you have this string method available for you.
Again, it's underscore, underscore, SDR, underscore, underscore, and this is your ability to return
whatever string representation you want when you're printing out your user defined object.
And we'll actually be using this a lot within Django because oftentimes we're going to be creating these
unique models and databases that when we actually want to display to a user an instance of that particular
data point, we'll probably have to customize what should actually be displayed or prints it out for
the user.
So that's the string method.
The other one that's commonly used is the length method.
So we say underscore, underscore, LPN, underscore, underscore and self.
And this one is a little more strict.
SDR should always be returning a string.
LPN should always be returning an integer.
So just keep that in mind, you can't actually return back a string or something like that has to be
an integer, because that's the way Python thinks about the length of things as complete units.
So what we're going to say here is I'm going to return, and if I wanted to, I could just return the
number ninety nine whenever somebody asked me for the length of a book.
Obviously, it's not super useful, but I do want to show you that it would technically work.
So if I now say, Hey, print the length of my book here, I mean, certain that's a little bit so we
can see that.
Then it's going to say, OK, let me look up the length of this object and it's going to return back.
Ninety nine.
So when you run that, it returns back ninety nine.
Clearly, we don't want this to be the same for every book.
Probably makes more sense for it to return the number of pages, so we'll say.
Self, that pages is going to be returned when you ask for the length of a book.
And so now when I print the length of my book, it says, OK, it's 120, it's up to you.
The context, the user and whoever is working with your Python file to see if just the number 120 actually
makes sense.
But keep that in mind.
You have the ability to define your own string representations of objects and the length of your objects.
And these are really the two special methods that we're going to be using quite a bit when we talk about
creating our own databases and models within Django.
We'll be storing things like books in our own little database.
And then later on, maybe we want to display on the web page some string representation of the object,
in which case it's really useful to actually show something like this.
All right.
That's it for special methods.
I'll see you at the next lecture.