So, here I am, back at the code, and
here I can see my class movie, and within it
I will begin to define, the function, init.
Now, there is something very curious about this function already,
which is that, there are two underscores on both
sides of the word init. These underscores are a
way for Python to tell us, the programmers, that hey,
the name init, is essentially reserved in Python, and that,
this is a special function or method. What's
special about init? Is that, it initializes or creates
space in memory. To remember details like title, story
line, et cetera, for new movies that we are
about to create. Now init, takes a few
pieces of information or arguments. The first of which,
is always the python keyword, self. Now, what is
this thing called, self, you ask? Well, you can
think of self as itself, or the instance being created.
So, when we run this line of code in our
other Python file, by the way, just to remind you,
here we are trying to create a new movie called, toy_story.
And when we run this line of code, we know
that the __init__ function inside the class Movie gets called. Self,
in that case, points to toy_story. Now here, I have
a confession to make. I remember when I was first learning
object oriented programming years ago in college. This concept of
self, was really difficult for me to understand. If that
is the case with you, don't worry about it too
much for now. This will get really clear when we
learn this entire piece of coding a few seconds. In
any case, let's add the keyword self, to our list
of new words. Okay, I'm going to go back to
the class movie, and continue to define the function, init.
Now, we want init, to initialize pieces of information like title, story line
and others that we want to remember inside our class. Here's a way to do that. I
will say, self.title, then self.storyline.
The next one is, poster_image_url, and
finally, trailer_youtube_url. Now, we have to somehow initialize
these variables, with information init is going to
receive. And in particular, it's going to receive, four
pieces of information. The first of which is,
a movie title, next is, the movie storyline,
then the poster image of the movie, and
finally, the trailer on YouTube. So,get this, once I've
received these four pieces of information as arguments to
the __init__ function, I can initialize my Movie variables.
Here's how to do that. self.title, is the movie
title. Self.storyline, is the movie story line. Next is,
the poster image. And finally, the trailer
on youtube. Now here, I have a confession to
make. Which is, that we've written a lot of
code that looks new and strange. Well, the one
thing I can say is that, we should all
just take a pause and a deep breath together.
For now, all I want you to do, is
pause this video, and write down this code on your
computer. All of this code will make sense as soon
as we run this program. Now, there is one key point
I would like to highlight, before I execute this piece
of code, which is, that if I go to my
other Python file, which is entertainment_center.py. And run this piece
of code. It will throw an error. The error will happen,
because when we try to create the movie, Toy Story,
the init function gets called, which is now
looking for several arguments or pieces of information, although,
we are not sending it any information whatsoever. So
let me change that. Now, I know that first
argument for the init function is, self. Which is
added by default in Python. So I will skip
it. The next one is movie title, which I know
is, Toy Story, so I will add that. After that, is the movie
storyline, so I will type that in next. A Story. Of
a boy and his toys that come to
life. The next one is, the poster image, so I will add that.
I had previously gone on Wikipedia and found a link to, Toy Story's
poster, so I added that, and finally, the Youtube trailer, which I will add also.
There it is. Now, let me just scroll
back so we can see the beginning of the
code. Here, I recognize that you can't see all
of the code, so what I've done is, take an
image of the code, and put it in the
instructor notes for you to look at. All right,
the last thing I'm going to do here, is
print Toy Story's story line. Let me save this file,
and then go back to my class Movie file, and save that one also. And then
come back again, to my entertainment_center.py file. Now
here, if everything goes correctly, my program should
print out, toy_story.storyline. Let's run it. And there
it is, Toy story, storyline is getting printed.
Okay, so, in the next video we are
going to see what happens behind the scenes when
we run this piece of code.