Organizing code with functions
A module is a .py file containing functions and variables designed to be reused by other programs.
The Reuse Problem
Suppose several programs need the same function. Without a reusable module, the function may have to be written separately in each program. That creates duplicated code: several copies of the same functionality that must be maintained independently. Modules address this problem by allowing a function to be written once and used by many different programs.
A Module as a Reuse Container
A module is a .py file containing functions and variables designed to be reused by other programs.
The important idea is not merely that a module has a .py filename. Its purpose is to hold shared code. Functions and variables placed in the module can then be used by multiple programs. The module becomes a single source of truth for that shared functionality: instead of keeping separate copies in several programs, the shared definition is maintained in one place.
Creating the Simplest Module
- 1Create a file with the .py extension.
- 2Give the file a descriptive name.
- 3Place regular Python code, such as functions and variables, in the file.
- 4Have other programs import and use the module.
A Shared Conversion Module
Three programs need the same conversion function.
- 1Choose the module
Create a descriptive .py file named
conversions.py. The file is intended to hold code that other programs can reuse. - 2Place the shared function in it
Write the conversion function in
conversions.pyrather than copying it into each of the three programs. - 3Connect the programs
Each program imports and uses the conversions module. All three programs now use the shared function from one module.
- 4Maintain one definition
If the shared function needs to be maintained, the module provides one place for that shared functionality instead of three duplicated copies.
One descriptive .py file provides shared functionality to multiple programs and acts as the single source of truth for that functionality.
Module or Script
| Aspect | Module file | Regular Python script |
|---|---|---|
| File type | A .py file | Python code in a file |
| Primary purpose | To contain functions and variables for reuse by other programs | To serve as a program rather than as shared code for other programs |
| Relationship with other programs | Other programs import and use it | It is not being described here as the shared module used by other programs |
| Organization goal | Maintain shared functionality in one source | Provide code for the script's own purpose |
The central difference is intended role: a module is organized for reuse by other programs.
A module file and a regular Python script can both contain regular Python code and use the .py file extension. The distinction taught here is their intended relationship to other programs. A module is designed to be reused: other programs import and use its functions and variables. A regular script is treated as a program in its own right rather than as the shared source for several other programs.
Mistakes Beginners Make
Common mistake Thinking a module requires a special kind of Python code
What it looks likeA learner assumes that ordinary functions and variables cannot be placed in a module.
Why it failsThe simplest way to create a module is to write regular Python code in a .py file.
Do this insteadStart with a descriptive .py file containing the functions and variables intended for reuse.
Common mistake Copying the same function into every program
What it looks likeSeveral programs each contain their own copy of the same shared function.
Why it failsSeparate copies duplicate code and do not provide one source of truth for the shared functionality.
Do this insteadPlace the function in one module and have the programs import and use that module.
Common mistake Focusing only on the .py extension
What it looks likeA learner decides that every .py file has the same role.
Why it failsThe important distinction is the file's intended purpose. A module is a .py file designed to be reused by other programs.
Do this insteadAsk whether the file is organized as shared functionality for other programs or as a program in its own right.
Practice the Design Choice
A team has four programs that need the same function and the same supporting variable. Should the team place four copies in the programs or create one reusable module? Describe the file they should create and explain how the programs would use it.
Hints
- Look for the option that avoids duplicated code.
- The shared file should use the .py extension and have a descriptive name.
- The programs should import and use the shared module.
Checking the Answer
Choose an organization for four programs that share one function and one variable.
- 1Reject duplication
Four separate copies would repeat the shared code and make the functionality less centralized.
- 2Create one module
Write the function and variable in one descriptive .py file.
- 3Reuse the module
Each program imports and uses that same module.
The one-module design keeps the shared functionality in a single source that all four programs can use.
Key Takeaways
- 1A module is a .py file containing functions and variables designed for reuse by other programs.
- 2Modules reduce code duplication by allowing a function to be written once and used in many programs.
- 3The simplest module is a descriptive .py file containing regular Python code.
- 4Multiple programs can import and use the same module, keeping shared functionality in one source of truth.
- 5The main difference between a reusable module and a regular script is intended purpose and relationship to other programs.
At a glance
- A module organizes reusable functions and variables in a .py file.
- Modules solve duplication by letting many programs use one function defined once.
- Creating a module starts with regular Python code in a descriptively named .py file.
- A module is intended for use by other programs, while a regular script is treated as a program in its own right.