Simply put, a module is a file consisting of Python code. It can define functions, classes and variables and can also include runnable code. A Python file can be referenced as a module. It has an extension “.py”. Python is a powerful language that allows developers to build amazing things but it can gain even more functionality with the use of modules. Modules are building blocks for adding functionality to your code as a developer so you don’t need to continually redo everything.
A Python module contains statements and definitions. It can also contain both executable statements and functions. A file called test.py can be a module named “test” and contains runnable code encompassing of various statements, functions and definitions.
Modules come from modular programming. This means that the functionality of code is broken down into parts or blocks of code. These parts or block has great advantages which are scoping, reusability and simplicity.
Scoping means that modules create a separate namespace meaning that two different modules can have functions with the same name and importing a module makes this part of the global space in the code being executed.
Reusability is the most important advantage of modularity. When you write a piece of code, modules help you avoid the need to write all the functionalities that you may need. This also helps avoid duplication which is essential because duplication of efforts uses more computer memory and makes it less efficient.
When modules have a little dependency on each other, it helps achieve simplicity. Such a module is built with a simple purpose in mind. Simplicity also helps in avoiding inter-dependency among these modules.
Some modules are already built into the standard python library. These kind of modules are referred to as “Built-in modules”.
Modules are imported only once during execution. The module is normally defined at the beginning of the code, but you can also define it at any point in the code. Since code execution in Python is serial, meaning it follows a required pattern, you must import the module first before you execute any function inside of it. Modules can also be executed from within the function. This means that code inside that module can only be used once the function is executed.
In this article, we covered what modules are, where they come from, their advantages, types of modules and how to import and execute modules. So, why not embrace this concept of modules, research further and practice. Wishing you the best of luck, our dear Developer!
Introduction In the fast paced world of technology, learning a versatile and high-in-demand programming language…
Introduction User Authentication policy is a very crucial process for every application and organization. It…
Introduction In previous articles, we have learnt about Django, how it works and how we…
Introduction In this article, we shall learn how to build and implement a blog app.…
Introduction In this article, we shall use a database for the first time to build…
Introduction In this article, we will build a pages app that has a homepage and…