YouTube

Got a YouTube account?

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

English subtitles

← Updating the Design for Class Movie - Programming Foundations with Python

Get Embed Code
7 Languages

Showing Revision 4 created 05/25/2016 by Udacity Robot.

  1. So in a previous lesson we created a class

  2. called movie. Now this class had the following attributes,
  3. the movie's title, its storyline, a link to the
  4. movie's poster image, and a link to its YouTube
  5. trailer. Now in addition to these things, the class
  6. movie also had a function called show_trailer. After we
  7. had defined this class movie we created several instances
  8. of this class. Instances like Toy Story and Avatar.
  9. Now further imagine that we wanted to create
  10. another class called TvShow. I would think that
  11. this class would have details like the title
  12. of the show, its season and episode number. And
  13. also, the TV station that this show is
  14. aired on. Additionally, this class could also have
  15. a function called, get_local_listings. Once we've created a
  16. class called TvShow, we can create multiple instances of
  17. this class, instances like, season one, episode one
  18. of Breaking Bad, and the final episode of Seinfeld.
  19. Alright, so if we continue our thought experiment
  20. here, we can further imagine that there can be
  21. several items that both of these classes can
  22. share amongst each other. Things like title for sure,
  23. also things like duration of the movie and
  24. duration of the TV show in minutes. So the
  25. best way to structure this code would be
  26. to create another class called Video, which would
  27. have two attributes. The video's title, and the
  28. video's duration. And the class Movie, could inherit from
  29. this class Video. To do this, we would
  30. have to add the class name Video, inside these
  31. parentheses. Now, this would mean that class Movie,
  32. would inherit title and duration, from class Video. Additionally,
  33. the class TvShow could also inherit from class
  34. Video. To do this we would also have
  35. to add the class name Video inside these
  36. parentheses. Now this would mean that class TvShow would
  37. inherit title and duration from class video. Now
  38. you can clearly see how we can write a
  39. piece of code. In this case, class Video,
  40. and continue to reuse it in multiple different places.
  41. Another big benefit of writing code this way,
  42. in addition to just reusing code, is that
  43. it allows us to follow an intuitive model
  44. of how things exist in our brain. So intuitively
  45. speaking, we know what videos are, we also know what TV shows and movies are. So
  46. writing code in this way allows programmers to
  47. map how things exist in our brain onto code.