Python is a high-level programming language that supports various programming paradigms. It has a large ecosystem of libraries and third-party packages to support various functionalities. We can import modules to include external libraries and modules in our program and use their functionalities.
A custom module is a facility provided by Python to create a Python file containing reusable functions and classes that can be imported and used in different code files. It provides a more readable code interface and consequently improves maintainability.
Create a Python file for the module we need to import. Keep a relevant name and define all the required functionalities in it. For example, a validate.py
file that contains validation functions.
def validate_num(num):if len(num) == 11:return Truedef validate_pass(password):if len(password) < 7:return False
Create a Python file for the program where the created module will be imported and used. For example, a main.py
file that checks for the validity of a number
and a password
.
#need to import custom module hereprint("Let's check for validity :\n")number = '03001234567'#need to add code herepaasword = 'key123'#need to add code here
Import the created module to the code file and use its functionalities according to the requirement. We can import the whole module or a single function from the module.
The whole module is imported to the main.py
file, which means that we can access all the functions and classes available inside the module and use them in our code. We can access the functions inside the module using the dot operator
i.e. moduleName.functionName
to specify from where the function is accessed.
We can import the custom module by using the import
keyword and specifying the module's name.
import validate
In this example, we import
the validate
module and use its validate_num()
and validate_pass()
functions to check the validities in our code.
#import the whole custom moduleimport validateprint("Let's check for validity :\n")number = '03001234567'#access the function insdie the imported moduleif validate.validate_num(number):print('num is valid')else:print('num is invalid')password = 'key123'#access the function insdie the imported moduleif validate.validate_pass(password):print('password is valid')else:print('password is invalid')
A specific function is imported from the whole module to the main.py
file, which means that we can access the specified function in our code. We can import more than one function from a single module i.e. import function1 , function2 , function3
when there is a specified need for functions.
We can import a function from the custom module by using the from
keyword to specify the destination module of the function and import
keyword followed by the function's name.
from validate import validate_num
In this example, we import
the validate_num()
function from the validate
module and use it to check the validity of numbers in our code.
#import function from a custom modulefrom validate import validate_numprint("Let's check for validity :\n")number = '03001234567'#use the imported functionif validate_num(number):print('number is valid')else:print('number is invalid')
Custom modules improve reusability and code accessibility by allowing users to store necessary functions in one place and use them according to their requirements. If the module is large and needs specified functions only, it is preferred to import functions. However, importing the whole module also works equally well.
Why must we access a function using the dot operator
if the whole module is already imported?
Free Resources