Modularization on Python

Modularization with Function and Package

Modularity refers to the concept of making multiple modules first and then linking and combining them to form a complete system. So, modularization on Python is how we handle the project, by creating modules so it is easier for us or other developers to understand the code and project. In this article, we’ll talk about 2 kinds of modularization. Modularization with Function and modularization with Package.

Modularization with Function

The module is a file that contains python statements and definitions. You can call file *.py as a module. This file contains functions that may perform nothing when we execute it but will execute the function when it is called. Below is the screenshot from my project on pycharm.

Pic 1

The **_init.py_** file is the module. I collapse the function just for simpleness to understand the module. As you can see below, the file **_init.py_** contains two functions ekstraksi_data() and tampilkan_data(). If we run **_init.py,_** nothing will happen because those two functions are not executed.

Pic 2

But if we called it from other modules in this case main.py, the function will be executed.

Pic 3

To call any module use keyword import and to use it on our code just type ModuleName.FunctionToCall. Now you already found the advantage of using the module. Even writing hundreds or thousands of code lines you will not be confused whether it’s the variable that you made, or it’s from other modules.

Modularization with Package

We already understand the module, now we go one level up the module which is a package. The package is more like a directory, that holds multiple modules. You can call it using a keyword from. In Pic 1 above, we can see that gempaterkini is the package. The example to call a function inside that package is

from gempaterkini import ekstraksi_data, tampilkan_data

But personally, I do not prefer this as on your code the variable name only ekstraksi_data() and tampilkan_data(). Maybe this will reduce the recurrence of the same word and make your code smaller. But with a glance of an eye, you wouldn’t know where that variable on your code comes from.

if __name__ == '__main__':
    print('Aplikasi utama')
    result = ekstraksi_data()
    tampilkan_data(result)

That’s why I prefer using the import keywords.

import gempaterkini

By just using import keyword, you will need to add the name of the module/package + dot before the function that you call as below code.

if __name__ == '__main__':
    print('Aplikasi utama')
    result = gempaterkini.ekstraksi_data()
    gempaterkini.tampilkan_data(result)

By creating a package or module, you can also contribute your code to pypi.org. You can also find a lot of projects on this site. Don’t forget, before creating a module from scratch look at this site first. Check if you can find already made modules, so you can save your time for what matters.

Pic 4

It’s up to you how to call a function on your code, but modularization is a good start for your project. On a bigger project, it will be more useful as you may use lots of modules in one project. It start to become more interesting now. We’ll use the already made package on the project.

Until then, see you on the next stories.

Reference

https://www.geeksforgeeks.org/understanding-code-reuse-modularity-python-3/

https://innovationyourself.com/modularization-and-packages-in-python/

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics