liveBook · Manning

Module

As explained in Get Programming: Learn to code with Python

A separable software component, typically a file, that groups code and provides a limited unit of functionality.

A module is a manageable piece of a larger program that handles a particular task. Think of it as a black box: to use it, you need to know what input it takes, what job it does, and what output it gives back—not every detail of how it works. section 20.2

When dividing a program into modules:

  • Give each module a specific task so you can write and test it separately. section 20.2
  • Pass information between tasks through inputs and outputs rather than relying on what another task creates for itself. section 20.3
  • Aim to write a module once and reuse it with different inputs. section 20.4

A group project

Imagine assigning two people separate research tasks. You need their findings for the final presentation, but you do not need to direct every step of their research. Each person works independently and shares a result—much like a module completing a subtask. section 20.3

In Python, a module can also mean a file containing definitions. If shapes.py defines Circle and Rectangle, another file in the same folder can use `import shapes` to bring in those definitions. Libraries group one or more modules. section 35.1

Figure 35.1. Two files: shapes.py and test.py in the same folder. One file defines classes for Circle and Rectangle. Another file imports the classes defined in shapes.py and uses them by creating different objects of those types and changing their data attributes.

See how one file defines classes while another imports and uses them. section 35.1

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines modules through inputs, tasks, and outputs, then describes independent environments; defers code examples of information passing.
Examples
Illustrates modules with a group research project and two Python files sharing class definitions.
What it assumes
Introduces task division from familiar activities before relating modules to Python files containing class definitions.
What you can do after
Apply it via Python imports to use definitions from another file, and practice dividing larger tasks into reusable pieces.
How it connects
Connects modules to abstraction, input and output, functions, Python imports, and libraries.

Also explained in

Compare how 35 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.