Concepts / Organizing code with functions

Organizing code with functions

A module is a .py file containing functions and variables designed to be reused by other programs.

  • Programming

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.

stored inimported byimported byimported byReusable functiondefined onceshared_tools.pyfunctions and variablesProgram Auses the moduleProgram Buses the moduleProgram Cuses the module
How does a function defined once in a module become available to several 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.

containscontainsis used bymakes behavior available tomakes data available toModule file.py fileFunctionsreusable behaviorOther programuses shared codeVariablesreusable data
What contains reusable functions and variables, and how are those contents connected to the programs that use them?

Creating the Simplest Module

  1. Create a file with the .py extension.
  2. Give the file a descriptive name.
  3. Place regular Python code, such as functions and variables, in the file.
  4. 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

AspectModule fileRegular Python script
File typeA .py filePython code in a file
Primary purposeTo contain functions and variables for reuse by other programsTo serve as a program rather than as shared code for other programs
Relationship with other programsOther programs import and use itIt is not being described here as the shared module used by other programs
Organization goalMaintain shared functionality in one sourceProvide 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.

imported and used byimported and used byModule fileshared .py codeProgram Auses moduleProgram Buses moduleRegular scriptprogram file
What differs between a reusable module file and a script considered as a program?

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

EASY

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

  1. A module is a .py file containing functions and variables designed for reuse by other programs.
  2. Modules reduce code duplication by allowing a function to be written once and used in many programs.
  3. The simplest module is a descriptive .py file containing regular Python code.
  4. Multiple programs can import and use the same module, keeping shared functionality in one source of truth.
  5. 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.