-
Title:
Updating the Design for Class Movie - Programming Foundations with Python
-
Description:
-
So in a previous lesson we created a class
-
called movie. Now this class had the following attributes,
-
the movie's title, its storyline, a link to the
-
movie's poster image, and a link to its YouTube
-
trailer. Now in addition to these things, the class
-
movie also had a function called show_trailer. After we
-
had defined this class movie we created several instances
-
of this class. Instances like Toy Story and Avatar.
-
Now further imagine that we wanted to create
-
another class called TvShow. I would think that
-
this class would have details like the title
-
of the show, its season and episode number. And
-
also, the TV station that this show is
-
aired on. Additionally, this class could also have
-
a function called, get_local_listings. Once we've created a
-
class called TvShow, we can create multiple instances of
-
this class, instances like, season one, episode one
-
of Breaking Bad, and the final episode of Seinfeld.
-
Alright, so if we continue our thought experiment
-
here, we can further imagine that there can be
-
several items that both of these classes can
-
share amongst each other. Things like title for sure,
-
also things like duration of the movie and
-
duration of the TV show in minutes. So the
-
best way to structure this code would be
-
to create another class called Video, which would
-
have two attributes. The video's title, and the
-
video's duration. And the class Movie, could inherit from
-
this class Video. To do this, we would
-
have to add the class name Video, inside these
-
parentheses. Now, this would mean that class Movie,
-
would inherit title and duration, from class Video. Additionally,
-
the class TvShow could also inherit from class
-
Video. To do this we would also have
-
to add the class name Video inside these
-
parentheses. Now this would mean that class TvShow would
-
inherit title and duration from class video. Now
-
you can clearly see how we can write a
-
piece of code. In this case, class Video,
-
and continue to reuse it in multiple different places.
-
Another big benefit of writing code this way,
-
in addition to just reusing code, is that
-
it allows us to follow an intuitive model
-
of how things exist in our brain. So intuitively
-
speaking, we know what videos are, we also know what TV shows and movies are. So
-
writing code in this way allows programmers to
-
map how things exist in our brain onto code.