Concepts / Functions and how to define them

Functions and how to define them

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

  • Programming

From repeated code to reuse

Suppose several programs need the same function. Writing that function separately inside every program creates duplicated code. A module solves this by giving you one place to write the function and allowing many programs to use it.

The central idea is single-source reuse: write shared functionality once, then make it available to multiple programs through a module.

One module, many programs

reusereuseutility.pyshared functions andvariablesProgram Auses shared functionalityProgram Buses shared functionality
How does a function defined in one Python module become available to multiple other programs?

The module is the shared source. It contains the function once. Each program that uses the module can rely on that shared functionality instead of keeping its own separate copy. If the shared function needs to be improved, the module is the single place associated with that functionality.

What a module contains

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

containscontainsreused byreused byPython module.py fileFunctionsreusable behaviorVariablesreusable dataOther programsusers of the module
What is contained inside a Python module, and how does that content relate to programs that use it?

The defining feature of a module is not merely that it has a .py extension. Its contents are intended to be reused by other programs. Functions provide shared functionality, while variables provide shared values or data that other programs may use.

Creating a module

  1. Create a Python file with the .py extension.
  2. Give the file a descriptive name.
  3. Place the reusable functions and variables in that file.
  4. Use the file as the shared source for programs that need those functions or variables.

The simplest way to create a module is to write regular Python code, such as functions and variables, in a .py file with a descriptive name. A special module-building process is not required by the source definition: the reusable Python file itself is the module.

Module or standalone script

AspectModuleStandalone script
File formA .py fileA Python file
Main intentionProvide functions and variables for reuse by other programsRun as its own program
Relationship to other programsOther programs use its shared functionalityIt is treated as the program being run
Code duplicationHelps reduce duplication by centralizing shared functionalityMay contain functionality that is not organized for reuse elsewhere

A shared greeting function

Three small programs need the same greeting behavior.

Identify the repeated functionality: The greeting behavior is needed in more than one program, so keeping separate copies would duplicate code.

Place the function in one module: Create a descriptively named .py file and put the reusable greeting function there.

Reuse the module: The three programs use the shared module rather than maintaining three independent copies of the function.

Maintain one source: When the shared greeting function changes, the module is the central place associated with that functionality.

One module provides the shared function to multiple programs and avoids maintaining duplicated function definitions.

Mistakes about reuse

  • Copying the same function into every program that needs it.

    This preserves the duplication problem that modules are intended to solve.

    Fix: Place the shared function in one descriptive .py module and make the programs use that common source.

  • Assuming that every .py file is automatically being used as a reusable module.

    The source definition emphasizes that a module is designed to be reused by other programs.

    Fix: Treat the file as a module when its functions and variables are organized as shared functionality for other programs.

  • Creating a separate copy of shared functionality for each program.

    Separate copies prevent a single source of truth for the shared functionality.

    Fix: Keep the shared function in one module that multiple programs can use.

Check your understanding

EASY

You notice that four programs contain separate copies of the same function. Describe the simplest module-based improvement. Then explain why the improvement makes future maintenance less repetitive.

Hints
  • Start with the file type used for a Python module.
  • Think about placing the function in one shared source.
  • Relate your explanation to code duplication and a single source of truth.

What do you think happens?

A reusable function is needed by two programs. Which arrangement best matches the purpose of a module?

  • Keep one copy in each program
  • Put one copy in a descriptive .py file for both programs to use
  • Avoid placing the function in a file
Reveal answer

Answer: Put one copy in a descriptive .py file for both programs to use.

A module is a .py file containing functions and variables designed for reuse by other programs, so it helps replace duplicated copies with one shared source.

Key takeaways

  1. A module is a .py file containing functions and variables intended for reuse by other programs.
  2. Modules reduce code duplication by allowing one function to be written once and used in multiple programs.
  3. The simplest way to create a module is to place regular Python code in a descriptively named .py file.
  4. A module is distinguished by its reusable purpose, while a standalone script is intended to run as its own program.
  5. Using one shared module gives multiple programs a single source for common functionality.

Key Takeaways

  • A module is a .py file containing reusable functions and variables.
  • The main benefit of a module is avoiding duplicated function definitions across programs.
  • Creating a module requires only regular Python code in a descriptively named .py file.
  • The intended use separates a reusable module from a standalone script.
  • Multiple programs can share one module, maintaining a single source of truth for common functionality.