A module is a file that contains Python
programming. A module can be brought into another program via the
statement, or it can be executed directly as
the main script of an application program. There are two purposes for
modules; some files may do both.
- A library module is expected to
contain definitions of classes, functions and module variables. If
it does anything beyond this, it is generally hard to understand and
harder to use properly.
- A script (or application or
“main” module) does the useful work of an application.
It will have up to three disctinct elements: imports of any modules
on which it depeds, main function definitions, a script that does
the real work. It generally uses library modules.
Modules give us a larger-scale structure to our programs. We see
the following levels of Python programming:
The individual statement. A statement makes a specific state
change by changing the value of a variable. State changes are what
advance our program from its initial state to the desired ending
Multiple statements are combined in a function. Functions are
designed to be an indivisible, atomic unit of work. Functions can be
easily combined with other functions, but never taken apart.
Functions may be as simple as a single statement. While functions
could be complex, it's important that they be easily understood, and
this limits their complexity. A well-chosen function name helps to
clarify the processing.
Multiple functions and related data are used to define a class
of objects. To be useful, a class must have a narrowly defined set
of responsibilities. These responsibilities are characterized by the
class attributes and behaviors.
Multiple closely-related classes, functions and variables are
combined into modules. The module name is the file name. A module
should provide a closely related set of classes and functions.
Sometimes, a module will package a set of closely related
Optionally, modules can be combined into packages. The
directory structure defines the packages and their constituent
modules. Additionally, packages contain some additional files that
Python uses to locate all of the elements of the package.
The user-oriented application program depend on modules (or
The application — the functionality that the user perceives — is
usually the top-most “executable” script that does the
useful work. The relationship between shell commands (or desktop icons,
or web links) that a user sees and the packaging of components that
implement those commands can be murky. For example, a single application
file may have multiple aliases, making it appear like independed
commands. A single script can process multiple command-line
The application-level view, since it is presented to the user,
must focused on usability: the shell commands or icons the user sees.
The design of modules and packages sould be focused on maintenance and
adaptability. The modules are the files that you, the developer, must
use to keep the software understandable.
Components: Class and Module. A class is a container for attributes, method functions, and
nested class definitions. Similarly, a module can also contain
attributes, functions and class definitions.
A module is different from a class in several ways. First, a
module is a physical file; it is the unit of software construction and
configuration management. A class is defined within a file.
Additionally, a class definition allows us to create a number of class
instances, or objects. A module, on other hand, can only have a single
instance. Python will only import a module one time; any additional
requests to import a module have no effect. Any variables defined by the
module, similarly, will only have a single instance.
Beyond this technical distinction, we generally understand modules
to be the big, easy-to-understand components out of which are
applications are built. A class is a finer-grained piece of
functionality, which usually captures a small, very tightly bound
collection of attribute and operations.