Fastai-lesson 10 notes

Mark praire
AI³ | Theory, Practice, Business
5 min readAug 25, 2019

In this post i will talk about a very important skill we need in order to be able to follow along with the tutorial of fastai part 2 and this is “object oriented programming in python” why is is this important to understand? Well Jeremy uses OPP a lot in the lessons so if you don’t have a solid foundation, you can barely follow through so this post aim to give you a brief overview of what OOP is and hopefully will help you to follow along with the lessons.

Object: Instance of a Class

A class is like a blueprint: we can make many copies of our class.
When we do this, we say that we are instantiating our class. These instances are called objects.
Here is an example of class instantiation:

object_name = ClassName()

The Init Method

init is an initialization method used to construct class instances in custom ways. In this exercise we will simply introduce the utilization of the method, and in subsequent ones we will do fancier things.

init job is to set up an object the way we want it before we pass anything in, its called automatically when an object is created and therefore takes in values outside of the object or set values within the object. Now it’s time to explore the special init method!

Now you know about the initialization method (init())! Notice that this method takes in as input argument the self keyword. Could we input other arguments? Try creating additional instances with different names!

Instance Variables

Class instances are useful in that we can store values in them at the time of instantiation. We store these values in instance variables. This means that we can have many instances of the same class whose instance variables hold different values!
Example:

Notice that instance variables live in the body of the initialization method, as they are initialized when the object is instantiated. Also important to notice that they are preceded by self., as this is referring to the instance itself.

Multiple Instance Variables

We are not limited to declaring only one instance variable; in fact, we can declare many!

In this exercise we will declare two instance variables: identifier and data. Their values will be specified by the values passed to the initialization method, as before.

As you saw in this exercise, you can declare more than one instance variable! What kind of instance variables can you think of that might be useful to have in our DataShell class?

Class Variables

We saw that we can specify different instance variables.

But, what if we want any instance of a class to hold the same value for a specific variable? Enter class variables.

Class variables must not be specified at the time of instantiation and instead, are declared/specified at the class definition phase.

Class variables are different from instance variables (which we saw earlier). Even though class variables may be overridden, they are generally set even before object instantiation; therefore, class variable values across instances of the same class tend to be the same.

Overriding Class Variables

Sometimes our object instances have class variables whose values are not correct, and hence, not useful. For this reason it makes sense to modify our object’s class variables

Now you have learned how to override class variables. Does this look too different from changing the value of instance variables?

Methods l

We pass self.filename becuase is a class attribute, we don’t have to pass it explicited as a parameter

Not only are we able to declare both instance variables and class variables in our objects, we can also cook functions right into our objects as well. These object-contained functions are called methods.

Methods ll

We can do more interesting things with our objects’ methods. For example, we can interact with our objects’ data. In this exercise we will declare a method that prints the value of one of our instance variables.

Methods III

In this one, we’ll do something more interesting. We will add another method, avg(), which takes a list of integers, calculates the average value, and prints it out. To make things even more interesting, the list of integers for which avg() does this operations, is one of our object’s instance variables.

This means that our object can not only store data, but also can store procedures it can execute on its own data. Awesome

vim

Another important tool we need to have in our tool set is to have text editor like vim, i know some ppl find it hard to use but worry not vim team have you cover.

it is import that you has a DL engineer learn how to find answer quickly the best place to start before asking google is to use the “help” command, so if you time vim help it will give you a bunch of thing you can do, but the most important one is that vim comes with a tutor integrated so after you install vim you just type the command “vimtutor” in your bash and it will take you through a tutorial that only takes 30 min, 30 min is a small investment, compare the advantages of having a text editor in you command prompt
if you time vim:

in order to exit vim just type :qa!

Them go straight to the tutorial i talk about above by typing vimtutor:

And just follow along.

to learn more about the OOP check my repository here https://github.com/Petrojon/OOP-python

--

--