A few days ago, I started the Udacity course: “Programming Foundations with Python” for the sole purpose of having it completed before I apply for the A.I Nanodegree (AIND). Indeed, one of the requirements to get into the ND is to show some credentials in programming. The Python course is for beginners. It focuses on classes and functions, and it has multiple examples that shows how when to use functions and classes. The instructor is really good: he is concise and clear. After watching the videos and working on the problems, concepts like inheritance were much clearer to me. In this post, I will review how to define and work with functions and classes in python. I wrote this post mainly as an occasion to review the material of the course.
1. Functions
We will write a function that renames all the files in a directory. In our case, the function will remove all the digits that are contained in the original file name.
In listdir(r "C:/myfolder/")
, the letter r
stands for raw: it tells python that it must not interpret the characters in the string C:/myfolder/
.
- Abstraction: means to give names to things, so that the name captures the core of what a function or a whole program does.
2. Class
-
a class defines a template, a blueprint.
-
The name of a class starts with a capital letter. It is meant to differentiate with variables.
2.1. How to use an existing class: Turtle()
Before we start making our own class, let’s see how to use an existing class. We create a function draw_square
, that calls a class named Turtle()
.The goal of the function is to draw a square on a red background window.
-
With the line
brad = turtle.Turtle()
, we are saying to python that there is a file calledturtle
and inside that file, there is a class namedTurtle()
. -
The empty brackets
( )
is to call the funtion__init__( )
of the classTurtle()
. -
__init__()
function is called the constructor: it initializes/creates the space in memory for a new instance, a new object. In our case,__init__()
instantiates a new object namedbrad
. -
The underscore in
__init__()
tells us that the nameinit
is reserved in python: it is a special function / method. -
In the
draw_square()
function, we call several instance methods ofTurtle()
:forward
andright
.
2.2. Create a Class: Movie()
We have seen how to call a class and create new instances. Now, let’s build our own class from scrath.
-
In the “media.py” file, we create a class
Movie()
-
The 1st function in the class, is the constructor
__init__()
.
We first import the file media
.
-
toy_story = media.Movie()
, tells python to go to the filemedia
and get the classMovie()
, then call the function__init__()
. -
the function
__init__()
is there to instantiate (initialize), and creates space in memory for the instancetoy_story
. Anytime an object is created, it looks for an init function and calls whatever is in there. -
__init__()
takesself
as argument.self
points to the instance/objecttoy_story
.
Instance Variables
Typically, a movie has the following characteristics: a title, a storyline, a trailer and a poster image.
-
We are going to pass those values to the class as instance variables:
self.movie_title
, etc… These variables will be associated with every instance created. -
we need to let python knows that when we create an object
Movie( )
, the object will have the following attributes:movie_title
,storyline
, etc…
- Initialize the object:
__init__()
is going to receive several arguments:movie_title
,strotyline
,poster_image_url
,trailer_youtube_url
.
- Now, we can create a new instance of
Movie()
:toy_story
, and that has several arguments
Class variables
Class variables are used when instances to share variables.
- the name of the class variable is typically written in upper case
VALID_RATINGS
.
- The class variable can be called with:
Instance method
An instance method is a function that is defined inside a class and is associated with an instance.
- The first argument of an instance method must be
self
[for example,def show_trailer(self)
]. That tells python to run the function on this instance.
Special class variable: __doc__()
__doc__()
is used to print the documentation related to the Class
2.2. Class Inheritance : parent/child
So far, we have considered one type of videos, but let’s say we want now to create a class TVShow()
.
The attributes of the classes are:
- class Movie( ) : [title / duration / storyline / poster_image / youtube_trailer]
- class TVShow( ): : [title / duration / season / episode / tv_station]
There are several items that are shared by the 2 classes. We can create a new class Video( )
that will have title
and duration
as instance variables.
- we want
Movie( )
(the child) to inherite fromVideo( )
(the parent):Movie( )
will reuse everything that is available inVideo( )
, i.eMovie( )
will inheritetitle
andduration
. For this to happen, we need to callVideo( )
as an argument ofMovie( )
.
- we initialize the instance variables of
Video( )
by using :Video.__init__(self, movie_title, movie_duration)
Method overriding
If an instance method has the same name in the child and in the parent, then the instance method in child will override parent instance method. The child instance method will be ran in priority.
That will output:
Movie Title from Child: Toy Story