2. • Modular programming refers to the process of breaking a
large, unwieldy 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 to modularizing code in a
large application:
Simplicity: Rather than focusing on the entire problem at hand, a
module typically focuses on one relatively small portion of the
problem. If you’re working on a single module, you’ll have a smaller
problem domain to wrap your head around. This makes
development easier and less error-prone.
3. • Maintainability: Modules are typically designed so that they enforce logical
boundaries between different problem domains. If modules are written in a
way that minimizes interdependency, there is decreased likelihood that
modifications to a single module will have an impact on other parts of the
program. (You may even be able to make changes to a module without having
any knowledge of the application outside that module.) This makes it more
viable for a team of many programmers to work collaboratively on a large
application.
• Reusability: Functionality defined in a single module can be easily reused
(through an appropriately defined interface) by other parts of the application.
This eliminates the need to duplicate code.
• Scoping: Modules typically define a separate namespace, which helps avoid
collisions between identifiers in different areas of a program.
4. Python Modules:
• There are three different ways to define a module in
Python:
• A module can be written in Python itself.
• A module can be written in C and loaded dynamically at run-
time, like the re (regular expression) module.
• A built-in module is intrinsically contained in the interpreter,
like the itertools module.
5. The import Statement
• Module contents are made available to the caller with the
import statement. The import statement takes many
different forms, shown below.
import <module_name>
The simplest form is the one already shown above:
import <module_name>
6. 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
caller’s symbol table:
• from <module_name> import <name(s)>