Chapter-5 Python Module

Module

Till now we discussed about the Function, but at times we may require the Function defined in an Application to be used in another one. One way, is to redefine the Function once again at the required place. The other way is, to save the function as a Module and include it any Application in which it is required.

What is a Module?

The Module is nothing but a File where one or more related Functions and also Classes are defined. It is considered as Library file, which can be included in any Application.

Again like in Functions, Modules can also be of 2 types System Defined and User Defined.

The module has to be imported into the current program before using it. To import a Module use the following syntax.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import <module name>
import <module name>
import <module name>

Once the module is successfully imported we can use the function. But at times, a same Function name may be available in 2 or more modules. The better programing practice is to use the Function name along with Module name to avoid any ambiguity.

Characteristics of Module

  • The module can have any number of Functions defined within it.
  • Along with the Functions even Classes, Constants can also be defined in it.
  • Each Function or Class or Constant can be referred separately whenever re-quired.
  • It is not necessary to use all the Functions within the Module.
  • This is applicable to both User defined and System defined Modules
  • Few functions like input(), print(), type() are available without importing any module, these are Built in Functions that comes along with the language itself.

When Python environment is loaded few default modules are also loaded, which we need not import explicitly. These functions are available in “builtins” module. T view the Functions available in builtins, the procedure is, in the Interpreter prompt of Python type

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import builtins
import builtins
import builtins

and press enter, then type

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
dir (builtins)
dir (builtins)
dir (builtins)

this will list out the names of the default functions, methods, constants and classes available in the module “builtins.”. To know the syntax of any function under a module use HELP function. In the interpreter type,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
help (<function name>)
help (<function name>)
help (<function name>)

eg. help (print). To list out the System defined Modules available, in the interpreter type

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
help("modules")
help("modules")
help("modules")

We will get a display of Module Names in alphabetical order. While the DIR function lists out the Functions available in the loaded Module. The HELP function lists out the Syntax of a particular Function packages

While Modules are equivalent to Files in a System, The Packages are equivalent to Folders. Therefore referring any Function that is available in a Package, will be like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<package name>.<module name>.<function name>
<package name>.<module name>.<function name>
<package name>.<module name>.<function name>

Using FROM statement

To avoid writing such lengthy part of Package, Module and Function name each time, we can use the From Statement.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from <Module name> import <function name>
from <Module name> import <function name>
from <Module name> import <function name>

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from math import sqrt
from math import sqrt
from math import sqrt

So, here math is one of the module and sqrt(Square root) is one of the function in it.

If only a particular function is going to be used within our program, then we can just use

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import <module name>.<function name>
import <module name>.<function name>
import <module name>.<function name>

Both helps to refer a Function without referring the Module names or Package Names during usage. Just reference of Function name is enough, when a Module is imported. But good programing practice is to include the Module name also to avoid any clash of same Function names in different Modules as already discussed.

Using alias name for Modules in Import Statement

Instead of using the lengthy module name.function name each time we refer, it is possible to use Alias where we can replace the modulename.functionname with the aliasname.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import <module name> as <alias name>
import <module name> as <alias name>
import <module name> as <alias name>

Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import pandas as pd
import pandas as pd
import pandas as pd

Whenever we have to refer the Pandas(It’s a one of the module used for data cleaning), we can use the alias pd instead. This is even applicable to the Functions within the module.