Python Modules
Python Modules
• Modules
• Top-Down Approach
• Python Modules
Modules
• We can make complex systems more manageable by designing them as a set
of subsystems, or modules.
• Example: Bennett University System
Bennett University
Advantages of
Modules:
• Every module needs to provide a specification of how it is
to be used, which is referred to as the module’s interface.
• Any program code making use of a given module is called a
client of the module.
• A module’s specification should be sufficiently clear and
complete so that its clients can effectively utilize it.
Module • A docstring is a string literal denoted by triple quotes used
in Python for providing the specification of certain program
Specification elements.
• Example:
MCQs
1. Which of the following is not an advantage in the use of modules in software development?
a) Provides a natural means of dividing up programming tasks.
b) Provides a means of reducing the size of a program.
c) Provides a means for the reuse of program code.
d) Provides a means of separately testing individual parts of a program.
e) Provides a means of integrating parts of a program during testing.
f) Facilitates the modification of specific program functionalities.
2. A specification of how a particular module is used is called the module’s_______________.
3. Program code that makes use of a given module is called a _______________ of the module.
4. Indicate which of the following are true. A docstring in Python is
a) A string literal denoted by triple or double quotes.
b) A means of providing specification for certain program elements in Python.
c) A string literal that may span more than one line.
MCQs: Answer
1. Which of the following is not an advantage in the use of modules in software development?
a) Provides a natural means of dividing up programming tasks.
b) Provides a means of reducing the size of a program.
c) Provides a means for the reuse of program code.
d) Provides a means of separately testing individual parts of a program.
e) Provides a means of integrating parts of a program during testing.
f) Facilitates the modification of specific program functionalities.
2. A specification of how a particular module is used is called the module’s Interface.
3. Program code that makes use of a given module is called a client of the module.
4. Indicate which of the following are true. A docstring in Python is
a) A string literal denoted by triple or double quotes.
b) A means of providing specification for certain program elements in Python.
c) A string literal that may span more than one line.
• Top-down design is an approach for deriving a modular
design in which the overall design of a system is
developed first, deferring the specification of more
detailed aspects of the design until later steps.
• This stage of the design provides sufficient detail from which to implement the
program. The main module provides the overall construction of the program.
MCQs
1. In top-down design (select one),
a) The details of a program design are addressed before the overall design.
b) The overall design of a program is addressed before the details.
2. All modular designs are a result of a top-down design process.
a) TRUE
b) FALSE
3. In top-down design, every module is broken down into the same number of submodules.
a) TRUE
b) FALSE
4. Which of the following advantages of modular design apply to the design of the calendar
program.
a) Provides a means for the development of well-designed programs.
b) Provides a natural means of dividing up programming tasks.
c) Provides a means of separately testing individual parts of a program.
MCQs: Answer
1. In top-down design (select one),
a) The details of a program design are addressed before the overall design.
b) The overall design of a program is addressed before the details.
2. All modular designs are a result of a top-down design process.
a) TRUE
b) FALSE
3. In top-down design, every module is broken down into the same number of submodules.
a) TRUE
b) FALSE
4. Which of the following advantages of modular design apply to the design of the calendar
program.
a) Provides a means for the development of well-designed programs.
b) Provides a natural means of dividing up programming tasks.
c) Provides a means of separately testing individual parts of a program.
Python Module
• A Python module is a file containing Python definitions and statements.
• A module allows you to logically organize your Python code.
• A module can define functions, classes, variables and can also include
runnable code.
• Module Creation and Use: To create a module just save the code you want in a file
with the file extension .py.
• We can use the module we just created, by using the import statement.
def greeting(name): import mymodule
print("Hello, " + name) mymodule.greeting(“Bennett")
Module Creation and Use: Variables in Module
• The module can contain functions, as already described, but also variables of all
types (arrays, dictionaries, objects etc.).
Module Creation: Save this code in the Module Use: Import the module named
file mymodule.py mymodule, and access the person1 dictionary
person1 = { import mymodule
a = mymodule.person1["age"]
"name": "John",
print(a)
"age": 36,
"country": "Norway"
} Module Use: You can create the alias or rename
the module while using.
Create an alias for mymodule called mx
import mymodule as mx
a = mx.person1["age"]
print(a)
Module Creation and Use: Example
2
import platform
x = platform.system()
print(x)
• There is a built-in function to list all the function names (or variable names) in a
module, i.e. dir() function.
Example: List all the defined names belonging to the platform module:
import platform
x = dir(platform)
print(x)
Modules and Namespaces
• A namespace provides a
context for a set of
identifiers. Every module
in Python has its own
namespace.
• Namespaces provide a
means for resolving such
problems.
Modules and Namespaces: Use of Fully Qualified Function Names
• Two instances of identifier double, each defined in their own module, are
distinguished by being fully qualified with the name of the module in which
each is defined: module1.double and module2.double.
• Example:
Example:
Importing Modules
• In Python, the main module of any program is identified as the first (“top-level”)
module executed.
• With the import modulename form of import in Python, the namespace of the
imported module becomes available to, but does not become part of, the
namespace of the importing module.
• Therefore, the identifiers of the imported module, must be fully qualified
(prefixed with the module’s name) when accessed.
Import From Module
• You can choose to import only parts from a module, by using the from keyword.
• Syntax: from modulename import something
• Something can be a list of identifiers, a single renamed identifier, or an asterisk:
# mymodule from mymodule import person1
def greeting(name):
print("Hello, " + name) print (person1["age"])
person1 = {
"name": "John", Note: When using import modulename, the namespace
"age": 36, of the imported module does not become part of the
"country": "Norway" namespace of the importing module.
}
Note: With the from-import form of import, imported identifiers become part of the importing module’s namespace.
Because of the possibility of name clashes, import modulename is the preferred form of import in Python.
Import From Module: Example
Module Private Variables
• In Python, all the variables in a module are “public,” with the convention that
variables beginning with an two underscores are intended to be private.
• Public is accessible by any other module that imports it, whereas private is not
accessible in form of from modulename import *.
• Python does not provide any means for preventing access to variables or other
entities meant to be private.
• There is a convention that names beginning with two underscores (__) are
intended to be private.
Example: # mymodule
__def greeting(name):
print("Hello, " + name)
Thank You