A module in Python is an object that acts as a reference library. In short, each module is a file where various classes, functions, or variable definitions are stored. When developing large software, it is often necessary to perform different tasks, and using modules to separate these tasks makes the code easier to manage and remember.
Until now, all the programs we wrote were confined to a single file. For this example, we will need two files: one will be the module file, and the other will be the script file where the module will be called.
We will reuse the multiplication/division program we wrote earlier. Let’s write the code:
#Function Definition for multiplication
def multiply_function(x,y):
z=x*y
return z
#Function Definition for division
def division_function(x,y):
z=x/y
return z
Now, create a file named testmodule.py
and save it. Here, only the function definitions are provided, without calling them. In another file, we will import this module and call the functions from there. Write the following code and run the program:
# This program is an example of a module in python
import testmodule
print(testmodule.multiply_function(10,5)) #calling a function from a user defined module
print(testmodule.division_function(10,5)) #calling a function from a user defined module
50
2.0
The correct results are obtained, and the functions are simply called from a module.
Noteworthy points:
- To import a module, use the
import
keyword, and to access any function or variable, use the dot operator (.)
Aliasing a Module Name
In the above example, we used the module’s name directly. A module can also be defined with an alias instead of its original name using the as
keyword.
# This program is an example of a module in python
import testmodule as tm
print(tm.multiply_function(10,5)) #calling a function from a user defined module
print(tm.division_function(10,5)) #calling a function from a user defined module
Output:
50
2.0
Everything works correctly, only the name has changed.
dir() Function
A module can be large or small. In such cases, it is challenging to know what variables or functions are in the module. The dir()
function is used to solve this problem. When calling any module with this function, it shows what is inside that module. Let’s see an example:
import testmodule as tm
fn=dir(tm)
print(fn)
Output:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'division_function', 'multiply_function']
At the beginning, we see some system directories. Towards the end, we see the two functions we defined: division_function
and multiply_function
.
Importing a Specific Part of a Module
In the above example, when we imported the testmodule
module, everything was imported. If we want to import only a specific part, we can use the from
keyword. Let’s look at an example:
from testmodule import multiply_function as mf
print(mf(10,5))
Result:
50
Here, only the multiply_function
was imported using the from
keyword. Now, the division_function
will not work.