Modularization using Module, Package And Class




Helo, welcome to this lesson, in this lesson you will learning about how to organization the Python code to big scale project.  Conceptually just 3 type modularization on Python, first Module, then Package, and last Class.

I will explain from beginning, and the session on this course will always using everywhere, so this fundamental which you must know without doubt.

OK.. first open Your Pycharm, then create new project, and save it on the location where you want.

Untitled.png

1. Using Module

First Modularization on Python is Module, actually which we are call the Module is Python file.for example i will create a file Python on Pycharm:

2017-08-27_20-13-29.png

Then name the python file, on this i name it start.py.

2017-08-27_21-24-25.png

Then on previous about Module, actually which we call with module is the Python file.

2017-08-27_21-25-12.png

Look the code on the above, the start.py file is a module.

Then i will try to write a simple program, ie to store list of movie title on the module:

2017-08-27_21-28-40.png

not interesting when we look it..?, ok i will try to use for loops.

2017-08-27_21-31-02.png

already interesting..?

Then how if we want to separate the code…? i mean, we write daftar_film on another module, how to..?

First create a new module, on this i named it models.py.

2017-08-27_21-32-10.png

Then move the code on daftar_teman which on start.py module to new module we created (just cut it).

2017-08-27_21-35-12.png

Asked… how to call the code on start.py module which that is different module..?

The way is with using import function.

Back to start.py module, then adding this code:

from models import daftar_film

Or you can use automatic fitur on Pycharm, click the lamp icon on Pycharm, or you can also press ALT+Enter button on your keyboard:

2017-08-27_21-36-35.png

The result is:2017-08-27_21-38-21.png

After added import function to start.py module it will printing same output with the previous code. Explanation to code on the above is:

From models.py module include daftar_film.

Then i will try to create new module, i named it views.py.2017-08-27_21-39-01.png

Then move all code on start.py module to views.py module.

2017-08-27_21-41-24.png

Then create a new function on views.py module, i named the function the_views.2017-08-27_21-43-08.png

Explanation about function, the function is a block of code that do statements or one of block intructions which will executed when it called from others part on a program. One more about create function on Python !!!, dont forgot about identasion, beacuse it is mark of a block in the function

Then how to printing result of the code..?

I will call and printing it on start.py module, The way is importing views.py module on start.py module, then call the function which we create before on views.py module.

2017-08-27_21-47-57.png

As we look on the above, on start.py module we imported views.py module and too call a function which we create before, ie the_views() function.

Maybe you asked, why do we make it like this..?, why we do organization code in this way..?, is not just start.py module just call the_views and the_views just call daftar_film, etc, well..!!!, we will try it on next section, why we do like this.

 

2. Using Package

As we look on models and views module the names still generally, but that can’t imaging that’s is just models and views from daftar_film aplication, then to get next modularization i will create Package.

What is Package..?, if Module is the Python file, then Package is a directory.

There is different, that directory on Python is just regular directory, but the Package has special nature ie, as you see in the bottom:2017-08-27_21-59-48.png

2017-08-27_21-50-49.png

Let’s see here which we create a Package, then there is a module or Python file which we named it __init__.py.2017-08-27_21-56-48.png

And if we try to create a regular directory:2017-08-27_21-58-15.png

Then it just a empty directory without module or Python files.2017-08-27_22-08-27.png

So __init__.py file functionated to mark that is a package. If on a regular directory we create a module or Python file too, then that will be a package too.

Then we will deleting not_movie package which we create before, and then moving views.py and models.py module into movie package, the way is just drag and moved it to the package.2017-08-28_09-14-08.png

If we look on the above, then Pycharm automatic change import option be:

from movie.views import the_views

Explanation for code on the above is, from movie package go into views module and import the_views function.

The code format is as follows:

from package.module import apapun_yang_ingin_di_import_dari_module_ini

And if we run the code then will result same output as before

You still asked, for what difficulty like this we do..?, beacuse it will be easy if like this, because we can organization code more structured, maybe to small aplication like this still convenience to see, then how with big scale aplication which a thousand of code..?, if we created it on 1 module it will be confiuse and more diffiult to managing the code. For that we organized the code with in this way.

One more question, the how if on the daftar_film we want to adding more others data..?, e.g if we want to adding starring, duration etc, Ok we will introducing with Class.

3. Using Class

Class consep is part of paradigma is a programming perspective which are called with Object Oriented Programming (OOP), This OOP is very basic, you must know, when you know OOP in one of progamming language then you will applicable to other programming language, but the implementation just slightly different. OOP concept like this:

First open models.py module, as we look in code on the bottom the data is just title for a few movie:2017-08-28_10-33-50.png

Back to previous questions, how if we want to add others data like starring, duration etc..?, Ok.. let see it..2017-08-28_10-54-50.png

Explanation for code on the above is, on first line we make a Class which name the Class  is Movie (Generally Class name begins with uppercase). Then on the second line in the Class we make a function or method (if function which we declare on a Class then we call it method) which name the method is __init__(self)__init__() is a method which we called if a Class will create new variable, in this the variable is title and starring. In this __init__() have three parameters, ie self, title, starring(self) function is a parameter which we must be passed on each method (instance method) in Class, each function which we declare in a Class must be passed self argument. The self is pointer which it whill be pointed to the Class.

 

Then on third and fourth line there is self keyword before variable name pointed to global variable in a Class. So self.title = title meaning title is become an construktor argument assigned to title wich is global variable from Movie Class, and so on to next statement. 

On sixth line is a list which we create before, ie daftar_film list. And on the eight line create object from Movie Class, “The Only Strong” value will be copied to title atribute, and “Mark Dacascos” value will be copied to starring atribute. The object which created on computer memory then will be pointed by referention or film variable.

Then on the tenth line, daftar_film list we using append function to added film variable, which the variable to store data from Class which we create before.

And when we try to printing result from the code from start.py module, then the result will be printing as follows:2017-08-28_10-54-501.png

Why the data does not appear, why just look like this..?

 <movie.models.Movies object at 0x00C668F0)

What is it..?, Ok.. that is a memory address which stored data from daftar_film variable and it is form of Movie Class.

well.. we do not want something like this happen, we just want to know what is content from Movie Class..?, therefore we adding a new function:2017-08-28_11-36-511.png

On the sixth and seventh line as we marking on the above, i added new function ie:

def __str__(self):
    return 'Title = %s, Starring = %s' % (self.title, self.starring)

__str__ will be called by Python if Movie Class will be change to String. Then return usability is to return from __str__ function being a String, then we shape the pattern to be:

'Title = %s, Starring = %s' % (self.title, self.starring)

The explanation is, String title = %s, The %s string will be take data from self.title and also %s from Starring string will take data from self.starring.

Then on the ninth line is a list which we explanation before.

And on eleventh, twelfth and thirteenth line, on daftar_film list we adding Movie Class by usng append() function and we adding to few data.

Then how to if we want to add a new data, ie movie duration..?

How to do that..?

Of course we adding a new data..?..well.. we need to add a parameter on methid __init__(), like we look on the below:2017-08-29_08-17-10.png

* Adding new function to class

If we look code on the above, we just using built-in function from Python, then how if we want to add a function or new method on the Class..?, well, look at the code below:2017-08-29_08-29-39.png

As we look the code on the above, we added a new method, ie wathcing method, and in the method we make print() statement or to printing ‘Saya sudah menonton film %s, kamu bagaimana..?’

well.. %s here we take string data from title variable, and then on nineteenth line we make a nwe variable to store data from Movie Class which the data is new data will we insert to title, starring and duration variable, and the on twentieth line we called new method which we just created with statement:

nama_variabel.nama_fungsi()

or

sudah_nonton.watching()

And the result from the code will be printing at the top, like we look on like we look the output on the above.

* What is instance actually?

One more you sould know, for example there is code like this:

nama = 'Tri'
berat = 55
tinggi = 172.5
alamat = 'Harau'

nama, berattinggi and alamat is a variable, and Tri, 55, 173 and Harau is value from these variable, then hidden here is data type. Berat is a number 55 is an integer type, but in Python you dont need to notified that is an integer type, provided its given an integer value it will be instantly become an integer type.

And nama is a text, but in Python the text will be called string, so in some line you can know, where is variable, where is value and data type from the variable.

Ok.. back to Class, indeed when we created Movie Class, we created new data type, ie Movie.2017-08-29_08-59-08.png

Line which we marked on the above tell that is sudah_nonton variable has data type Movies, well.. on OOP paradigm which will be usefull if we learn about OOP on other programmin language, Movies called Class, and sudah_nonton called instance, so when we run that code will be result like this:2017-08-29_09-04-52.png

On __str__() method we marked that line be comment first, so on the code will be inactive. And explanation of the output i have marked is:

Movie Class containing data (“The Only Strong’,’Mark Dacascos’,’99 minutes’) that is instance from Movie Class which located on address memory as we marked on ouput the above, ie:

 <movie.models.Movies object at 0x00C563B0)

So.. two terms you should know in OOP concept is:

Class =  Data type
instance = Variable

Class is a sinonim from data type, and instance is a sinonim from variable.

* What is self?

Ok.. for additional explanation for self, i will take interesting explanation in this case:2017-08-29_10-50-07.png

The output would be error, yea..?. because method __init__() just need 3 variable, ie title, starring and duration. Whereas we added English as language to additional for data daftar_film variable. And interestingly from the error is:

TypeError: __init__() takes 4 positional arguments but 5 were given

On that error notified we given 5 argument, whereas clearly on that code we just giving 4, where is the 1 argument more..?, what does it mean..?

Well.. on hidden when we calling function from Class, the Python will inserted instance address from Movie Class on first part, so all the function from Class will have self on the beginning:2017-08-29_11-07-59.png

So.. if we say “The Only Strong” created with 4 argument, indeed Python make it 5 argument.

The conclusion is when we make function form Class property, add self variable on that. Then Python will inserted address from instance Class on each function call.

understanding about self neither easy nor difficult, or not overly importand, provided you understanding adding self on all function when it created in a Class.

* Last but not least, what is class variable?

Important benefits of self must should i explanation, if not will there will be OOP concept on Python be missed. I created title, starring and duration variable in __init__() function, extactly in instance address (self). But indeed you can create variable outside __ini__() function, like this:2017-08-29_11-48-47.png

Like code on  the above we created a variable outside __init__() function. Then on twenty third and twenty fourth lines we printing numbers of movies from daftar_film variable which the instance variable stored in __init__() function and we also displayed numbers of movies from JUMLAH variable which the variable located on outside __init__() function.

And on twenty third we count with a function on Python ie len() function which the function is to counting data on data type Dictionary, List etc. And on twent third line also there %d , and the function is to handel integer type.

Attention..!!!, JUMLAH variable on outside from __init__() function, so JUMLAH is variable from Movies Class.

I want to know in this code, how much movies from instance of Movies which has ben created, the way is adding this code:2017-08-29_12-05-11.png

The result if we adding code on the above is same with amounts of movies which we counting from variable which the variable stored on __ini__() function. Because __init__()  fucntion will be called when Movies created, because i want to updating number of Movies from Movies Class.

Anyone asked..?

Why not here..?
2017-08-29_12-10-52.png

Because it will be futile, if we created amount with self variable, the if you have four daftar_film then all the variable will having JUMLAH variable, even though the value certainly same. Then it will make more sense if the JUMLAH stored indeed Class variable.

So.. there are two concept, Class Variable = is variable related on Class, and..second Object variable or instance variable related on respectively object which created in __init__() method.

Ok.. just so far Class concept which that is sufficient to beggining meke programming on Python, of course still available Class Propertion which more advance which it rarely you need that, but if on future you meet that, with this concept is sufficient.

Ok.. sufficient fro Modularization which beggining from Module, Package then Class.

Make sure to truly understand the lesson before proceeding!