Importing modules
A module is a .py file containing functions and variables designed to be reused by other programs.
The Reuse Problem
Imagine that several programs need the same function. Without a module, you may copy that function into each program. If the function needs to change, each copied version must be found and updated. Modules solve this duplication problem by letting you write a function once and use it in many different programs.
A Module as a Reusable File
A module is a .py file containing functions and variables designed to be reused by other programs.
The important idea is not a special kind of Python function or variable. A module is created from regular Python code. Its distinguishing purpose is reuse: another program can import the module and use the functions and variables it contains. This gives several programs one shared source of truth for common functionality.
A module is both a file and a reuse boundary: the .py file holds shared functions and variables, while other programs import and use that shared code.
Creating the File
- Create a .py file with a descriptive name.
- Place the regular Python functions and variables that you want to reuse in that file.
- Have other programs import the module when they need those shared functions or variables.
Generated example: a developer could place commonly reused functions and variables in a descriptive file named shared_tools.py. A first program and a second program could both import that module instead of keeping separate copies of the same functionality.
What Importing Provides
When a program imports a module, the program can use the functions and variables designed for reuse in that module. The module remains the shared place where that functionality is maintained. If several programs use the same module, they all depend on that single source of truth rather than on separately copied versions.
Two Programs Sharing One Function
Two programs need the same function. How can their shared functionality be organized?
Choose the shared code: Identify the function that both programs need to use.
Place it in one module: Write the function in a descriptive .py file. That file is the module.
Import it in each program: Each program imports the same module instead of keeping its own copied version of the function.
Maintain one source: The shared function is maintained in the module, so the programs use one common source of functionality.
One module supplies reusable functionality to both programs, reducing code duplication.
Module or Script
| File role | Main purpose | Typical contents |
|---|---|---|
| Module | Provide reusable code to other programs | Functions and variables designed for reuse |
| Regular script | Run as a program directly | Python code organized for that program's own task |
Both a module and a regular script can be Python files. The distinction emphasized here is their purpose. A module is a .py file whose functions and variables are designed to be reused by other programs. A script is treated as a program to run directly rather than primarily as a shared source of functionality.
Mistakes with Modules
Copying the same function into every program
Copies create duplicated code instead of one shared source of functionality.
Fix:
Place the function in one descriptive .py module and have both programs import it.Assuming a module requires a special file format
The simplest module is created by writing regular Python functions and variables in a .py file.
Fix:
Use a descriptive .py filename and place the reusable Python code inside it.Treating every .py file as a reusable module
A module is specifically designed for reuse by other programs.
Fix:
Distinguish files by intended purpose: shared functionality for a module, or direct program execution for a script.
Practice and Summary
A team has three programs that need the same function. Describe where you would place the function, what kind of file you would create, and what each program would do to use the shared functionality.
Hints
- Think about the single source of truth.
- The simplest module is a descriptive .py file containing regular Python code.
- The programs should import the shared module.
- Modules reduce duplication by allowing one function to be used in many programs. The simplest module is a descriptive .py file containing regular Python functions and variables. Other programs import the module to use that shared functionality. A module is intended for reuse, while a regular script is intended to run directly as a program.
Key Takeaways
- A module is a .py file containing functions and variables designed for reuse.
- Modules let multiple programs use one shared implementation instead of copied code.
- The simplest module is made by placing regular Python code in a descriptively named .py file.
- Importing lets another program use the module's reusable functions and variables.
- A module is organized for reuse, while a regular script is organized to run directly.