Chap - 14
Chap - 14
Chapter
14
Modular Design
Key Topics
14.1. Introduction
14.1. Introduction
14.2. Software design Approaches
Modular design is a design approach that subdivides a system
14.2.1. Top Down Design into smaller parts called modules, which can be independently
14.3. Python Modules created and then used in different systems. This allows designs
14.4. Importing Modules In Python
14.4.1. Python Import Statement to be customized, upgraded, repaired and for parts to be reused.
14.4.2. Import with Renaming Modular design is a technique to divide a software system
14.4.3. Import Multiple Modules
14.4.4. Python from.... Import into multiple discrete and independent modules, which are
Statement expected to be capable of carrying out tasks independently. These
14.5. Python Built-in Modules
14.6. The Dir( ) Built-in Function modules may work as basic constructs for the entire software.
14.7. Python Packages Designers tend to design modules such that they can be executed
and/or compiled separately and independently.
Modular programming is the process of subdividing a
computer program into separate sub-programs. A module is a
software component or part of a program that contains one or
more routines. Similarly functions are grouped in the same unit
of programming code and separate functions are developed as
separate unit of code so that the code can be reused by other
applications. OOP is compatible with the modular programming
concept to a large extend. Here are some advantages of using
modular programming.
Advantage of Modular Programming.
1. Less code has to be written.
2. Faster development. Modular programming allows many
programmers to collaborate on the same application.
3. Easy debugging and maintenance as errors can easily be
identified, as they are localized to a subroutine or function.
14-2 Modular Design
4. Easy to understand as each module works independently to another module.
5. Several programmers can work on individual programs at the same time.
6. The scoping of variables can easily be controlled.
7. Modules can be re-used, eliminating the need to retype the code many times.
8. A single procedure can be developed for reuse, eliminating the need to retype the code many
times.
This does not enter the names of the functions defined in addmodule directly in the current
symbol table. It only enters the module name addmodule there. Using the module name we can
access the function using dot (.) operation. For example:
>>> addmodule.add(4,5.5)
Output
9.5
Python has a ton of standard modules available. These files are in the Lib directory inside the
location where you installed Python. Standard modules can be imported the same way as we import
our user-defined modules. There are various ways to import modules. They are listed as follows.
The import statement should be at the starting of code. The import keyword is followed by one
or more python module specifiers separated by commas. When the interpreter encounters an import
statement, it imports the module present in the search path.
import math
print("The value of pi is", math.pi)
Output
The value of pi is 3.141592653589793
Python interpreter execute module body immediately. Python module is loaded only once. To
access attribute so module, we use object as prefix. In the above example math is standard module
name and function pi is standard defined in module.
14-4 Modular Design
14.4.2. Import with Renaming
We can import a module by renaming it as follows.
import math as ma
print("The value of pi is", ma.pi)
Output
The value of pi is 3.141592653589793
We have renamed the math module as ma. Note that the name math is not recognized in our
scope. Hence, math.pi is invalid, ma.pi is the correct implementation.
14.4.3. Import Multiple Modules
In order to use multiple modules, consider the following example.
circle.py
def circle(radius):
print(3.14*radius*radius)
return
rectangle.py
def rectangle(len,bre):
print(len*bre)
return
display.py
import circle, rectangle
circle.circle(4)
rectangle.rectangle(3,5)
Output
50.24
15
Output
The value of pi is 3.141592653589793
Enter a Number6
Square root of no: 6 = 2.449489742783178
Factorial of no: 6 = 720
Example 2: areas.py
def circle(r):
print(3.14*r*r)
return
def square(r):
print(r*r)
return
def rectangle(len,bre):
print(len*bre)
return
Then create another file named calculateara.py in which we import all the attributes from
areas.
calculatearea.py
50.24
12
Module.Function Description
math.cos(x) Return the cosine of x radians.
math.sin(x) Return the sine of x radians.
Math.tan(x) Return the tangent of x radians.
math.pi The mathematical constant ? = 3.141592..., to available precision.
math.e The mathematical constant e = 2.718281..., to available precision.
Example
import math
dir(math)
Output
['__doc__', '__loader__', '__name__', '__package__', 'acos', 'acosh', 'asin',
'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e',
'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
'gamma', 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
Here all names that begin with underscore are default Python attributes associated with the
module.
Example 2
As we have defined a function add( ) in the module addmodule that we had in the beginning.
and circle( ), rectangle( ), and square( ) in the module calculatearea in example 2.
import addmodule
dir(addmodule)
Output
['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__',
'__loader__', '__name__', '__package__', 'add']
Here, we can see a sorted list of names along with add. All other names that begin with an
underscore are default Python attributes associated with the module and we did not define them
ourself.
14-8 Modular Design
import calculatearea
dir(calculatearea)
Output
['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__',
'__loader__', '__name__', '__package__', 'bredth', 'circle', 'len_rect',
'length', 'radius_cir', 'rectangle', 'square']
Here, we can see a sorted list of names along with circle, rectangle and square which are
imported from areas.py.