Python: Modules
According to the formal definition, a module stands for, one of a set of separate parts that, when combined, form a complete whole. Or "each of a set of standardized parts or independent units that can be used to construct a more complex structure". In programming, it is no different but part of a program that contains one or more procedures. A program is made up of one or more independently developed modules. For example, do you remember how in 12th standard the streams got specified into PCMB, arts, commerce, etc? Now roughly, physics, chemistry, maths or biology are modules that comprise to form the science stream. Small parts of s program combining to provide the whole functionality.
When it comes to big projects a major concern during development using traditional procedural techniques is that there are a number of complexities. Such projects involve thousands of lines of code and having a clear knowledge of what a segment of code does becomes more difficult.
Modularity was designed in order to overcome this problem and repetition of similar code at different steps in a program. Through modularity, codes are designed in segments to perform a specific function that can be referenced from anywhere in the program.
Some benefits of modularity are:
- Code Reusability
- Ease of maintenance
- Code Readability
- Easy Debugging
For instance, consider you want to develop a calculator app (Yeah not a biggie but easier to understand this way). You want to add basic functionalities of a calculator like addition, multiplication, subtraction, and division. So, we are going to make a module which will conatin these four functionalities:
You might be wondering how are we going to create a module? Simply writing a python code in a file, it is that easy.
So let's name this module calc. This calc module is going to contain all the functions that our calculator can perform.
After this, to use this one as a module, create another file as main.py and import the calc module in the main file.We need to use the import keyword in order to incorporate the module into the program.
We can import different functions and classes using the asterisk (*), as shown in the picture.
Or it can be done using an alias while using as keyword. For instance, here clac is imported as cl
The best part is if you have made a module, it's not necessarily be used for that particular file only. You can use it for another one as well if it matches the functionality.
Built-in modules
Built-in modules are written in C and are interpreted using a Python interpreter. Each built-in module contains the logic or the resources for certain specific functionalities such as os management, i/o operations, etc. The standard library also contains many Python scripts with .py extensions containing useful utilities.
To get the list of all Python modules, write command help('modules') and it will list all the built-in modules in Python.
In conclusion, a module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. Modules in Python are simply Python files with a .py extension and the name of the module will be the name of the file. A Python module can have a set of functions, classes or variables defined and implemented. Modules can help in making your code easier to understand. You can find a tutorial of modules on its official site as well.
You can explore more :
Writing for Humans and Machines
4 年You crack me up :-)
Software Developer | Angular - Node.js - SQL
4 年Oh! Your study of the python law is as fascinating as intriguing you are.