YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Defining init - Programming Foundations with Python

Get Embed Code
7 Languages

Showing Revision 9 created 09/10/2017 by Dejan Trajkovic.

  1. So, here I am, back at the code, and
  2. here I can see my class movie, and within it
  3. I will begin to define, [SOUND] the function, init.
  4. Now, there is something very curious about this function already,
  5. which is that, there are two underscores on both
  6. sides of the word in it. These underscores are a
  7. way for Python to tell us, the programmers, that hey,
  8. the name init, is essentially reserved in Python, and that,
  9. this is a special function or method. What's
  10. special about init? Is that, it initializes or creates
  11. space in memory. To remember details like title, story
  12. line, et cetera, for new movies that we are
  13. about to create. Now init, takes a few
  14. pieces of information or arguments. The first of which,
  15. is always the python keyword, self. Now, what is
  16. this thing called, self, you ask? Well, you can
  17. think of self as itself, or the instance being created.
  18. So, when we run this line of code in our
  19. other Python file, by the way, just to remind you,
  20. here we are trying to create a new movie called, toy_story.
  21. And when we run this line of code, we know
  22. that the __init__ function inside the class Movie gets called. Self,
  23. in that case, points to toy_story. Now here, I have
  24. a confession to make. I remember when I was first learning
  25. object oriented programming years ago in college. This concept of
  26. self, was really difficult for me to understand. If that
  27. is the case with you, don't worry about it too
  28. much for now. This will get, really clear when we
  29. learn this entire piece of coding a few seconds. In
  30. any case, let's add the keyword self, to our list
  31. of new words. Okay, I'm going to go back to
  32. the class movie, and continue to define the function, init.
  33. Now, we want init, to initialize pieces of information like title, story line
  34. and others that we want to remember inside our class. Here's a way to do that. I
  35. will say, self.title, then self.storyline.
  36. [SOUND] The next one is, poster_image_url, and
  37. finally, trailer_youtube_url. Now, we have to somehow initialize
  38. these variables, with information init is going to
  39. receive. And in particular, it's going to receive, four
  40. pieces of information. The first of which is,
  41. a movie title, [SOUND] next is, the movie storyline,
  42. then the poster image of the movie, and
  43. finally, the trailer on YouTube. So,get this, once I've
  44. received these four pieces of information as arguments to
  45. the __init__ function, I can initialize my Movie variables.
  46. Here's how to do that. self.title, is the movie
  47. title. Self.storyline, is the movie story line. Next is,
  48. [SOUND] the poster image. And finally, [SOUND] the trailer
  49. on youtube. Now here, I have a confession to
  50. make. Which is, that we've written a lot of
  51. code that looks new and strange. Well, the one
  52. thing I can say is that, we should all
  53. just take a pause and a deep breath together.
  54. [SOUND]. For now, all I want you to do, is
  55. pause this video, and write down this code on your
  56. computer. All of this code will make sense as soon
  57. as we run this program. Now, there is one key point
  58. I would like to highlight, before I execute this piece
  59. of code, which is, that if I go to my
  60. other Python file, which is entertainment_center.py. And run this piece
  61. of code. It will throw an error. The error will happen,
  62. because when we try to create the movie, Toy
  63. Story, the init function gets called, which is now
  64. looking for several arguments or pieces of information, although,
  65. we are not sending it any information whatsoever. So
  66. let me change that. Now, I know that first
  67. argument for the __init__ function is, self. Which is
  68. added by default in Python. So I will skip
  69. it. The next one is movie title, which I know
  70. is, Toy Story, so I will add that. After that, is the movie
  71. storyline, so I will type that in next. A Story. Of
  72. a boy and his toys that come to
  73. life. The next one is, the power image, so I will add that.
  74. [SOUND] I had previously gone on Wikipedia and found a link to, Toy Story's
  75. poster, so I added that and finally, the Youtube trailer, which I will add also.
  76. [SOUND] There it is. Now, let me just scroll
  77. back so we can see the beginning of the
  78. code. Here, I recognize that you can't see all
  79. of the code, so what I've done is, take an
  80. image of the code, and put it in the
  81. instructor notes for you to look at. All right,
  82. the last thing I'm going to do here, is
  83. type Toy Story's story line. Let me save this file,
  84. and then go back to my class Movie file, and save that one also. And then
  85. come back again, to my entertainment_center.py file. Now
  86. here, if everything goes correctly, my program should
  87. print out, toy_story.storyline. Let's run it. And there
  88. it is, Toy story, storyline is getting printed.
  89. Okay, so, in the next video we are
  90. going to see what happens behind the scenes when
  91. we run this piece of code.