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
- Create a file with the .py extension.
- Give the file a descriptive name.
- Place regular Python code, such as functions and variables, in the file.
- Have other programs import and use the module.
A Shared Conversion Module
Three programs need the same conversion function.
Choose the module: Create a descriptive .py file named conversions.py. The file is intended to hold code that other programs can reuse.
Place the shared function in it: Write the conversion function in conversions.py rather than copying it into each of the three programs.
Connect the programs: Each program imports and uses the conversions module. All three programs now use the shared function from one module.
Maintain 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 |
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
Thinking a module requires a special kind of Python code
The simplest way to create a module is to write regular Python code in a .py file.
Fix:
Start with a descriptive .py file containing the functions and variables intended for reuse.Copying the same function into every program
Separate copies duplicate code and do not provide one source of truth for the shared functionality.
Fix:
Place the function in one module and have the programs import and use that module.Focusing only on the .py extension
The important distinction is the file's intended purpose. A module is a .py file designed to be reused by other programs.
Fix:
Ask 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.
Reject duplication: Four separate copies would repeat the shared code and make the functionality less centralized.
Create one module: Write the function and variable in one descriptive .py file.
Reuse 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
- A module is a .py file containing functions and variables designed for reuse by other programs.
- Modules reduce code duplication by allowing a function to be written once and used in many programs.
- The simplest module is a descriptive .py file containing regular Python code.
- Multiple programs can import and use the same module, keeping shared functionality in one source of truth.
- The main difference between a reusable module and a regular script is intended purpose and relationship to other programs.
Key Takeaways
- 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.