Artificial Intelligence Design Health Innovation Machine Learning Technology

What is a module in Python?

Pinterest LinkedIn Tumblr

What is a module?

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.

What does a Python module contain?

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.

Where do modules come from?

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

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

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.

ALSO READ  Creating Your Own GitHub Account: How?

Simplicity

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.

Types of modules

Built-in modules

Some modules are already built into the standard python library. These kind of modules are referred to as “Built-in modules”.

How do you import and execute 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.

Conclusion

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!