Python Modules

What You Will Learn:

In python, Modules refer to a file containing Python statements and python function definitions.

Modular programming refers to the process of breaking a large programming task into separate, smaller, more manageable subtasks or modules.

Individual modules can then be cobbled together like building blocks to create a larger application.

There are several advantages of modules while developing a large application:

*  On dividing the large program into a number of small modules, the program becomes easy/simple to understand.

* development process becomes easier and less error-prone.

* different modules are independent of each other. (We may make changes to a module without having any knowledge of the application outside that module.)

* Reusability: Functionality defined in a single module can be easily reused

* This eliminates the need to recreate duplicate code.(code once is written can be reused again and again)

The import Statement

Module contents are made available to the caller with the import statement. The import statement takes many different forms, shown below.

Method:1

import <module_name>

This is the simplest form to import a module.

Method:2

from <module_name> import <name(s)>

An alternate form of the import statement allows individual objects from the module to be imported directly into the program.

Method:3

import <module_name>[, <module_name> …]

Several comma-separated modules may be specified in a single import statement.

Method:4

from <module_name> import *

It is even possible to indiscriminately import everything from a module.

This will place the names of all objects from <module_name> into the program, with the exception of any that begin with the underscore (_) character.

Method:5

from <module_name> import <name> as <alt_name>

It is also possible to import individual objects but import them into the program with alternate names:

from <module_name> import <name> as <alt_name>[, <name> as <alt_name> …]

This makes it possible to import directly into the program but avoid conflicts with previously existing names:

import <module_name> as <alt_name>

We can also import an entire module under an alternate name