What are the python modules?
Suppose you are creating a set of different games but these games have a common set of code working for all. As programs have heavy code it will be much messed up to save all the program in 1 file plus the developer will need to re-write the common code for every game individually which is a problem as it will take too much time. To solve these problems, modules can be used.
Introduction
Consider the module as a library of function, variable, and methods that contains a specific functionality. A Python module contains both Definitions and Statements.
It helps us to organize the code in the file. The module is used to break down the large programs into distinctly small and manageable code. It also provides the reusability of code. A file saved with the extension of “.py” is called module whereas the name of the file will be the name of the module.
Example: A file saved as “Mul.py” will be called a module. And the name of the module will be “Mul”.
To use the module:
Import Statement:
“Import” keyword is used to import source files from one module to another.
Syntax: - import module1, module2, module3,…..module n
import math,datatime
Here it will not import the function’s name or any specific function instead it will import the complete functionality of the module. To use a specific function from the module dot (.) operator is used.
Syntax: import module_name.function_name
Example:
import math.pi
Note that we can import multiple modules with a single statement.
From-import Statement:
It is used to import only specific attributes instead of the whole module into the namespace.
Syntax: - from <module name> import <name1><name2>………..<name n>
Example:
def add (a,b)
return(a+b)
def sub (a,b)
return(a-b)
def mul (a,b)
return(a*b)
To import the above-given module:
from calculation import add
In such cases, the dot (.) operator is not used to access function. We can call more than 1 attribute at a given time
Example:
from calculation import add, mul
One can also RENAME the module i.e. it is used to give a specific name to the import module so that it is easier for use in the source file. Syntax: import <module_name> as <specific_name>
eg: import calculation as cal
STANDARD MODULES:
Standard modules that can be used to import are:
Using dir() function:
It is used to show all the names of modules, variables, functions that is defined. It returns in the form of the sorted list of strings.
Syntax: import <module_name>
List= dir(module_name)
print(dir)
reload function:
As in the above stated that a module is loaded once regardless of number of times import to the source file. But if we want to import that module again then we used reload() function.
Syntax: reload(<module_name>)
global() and local() function:
It is used to return the names in global and local namespaces depending on the location i.e., within the function if local() is called then all the names that can be accessed locally from that functions or if global() is called then all the names that can be accessed globally from that function.
To know more about built-in modules:
Help(‘modules”)
Type this command in you python console or click here: Commonly used modules in python