Concepts / Functions and how to define them

Functions and how to define them

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

  • 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.

Key point

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

utility.pyshared functions andvariablesProgram Auses shared functionalityProgram Buses shared functionalityreusereuse
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

Definition

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

Python module.py fileFunctionsreusable behaviorVariablesreusable dataOther programsusers of the modulecontainscontainsreused byreused by
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. 1
    Create a Python file with the .py extension.
  2. 2
    Give the file a descriptive name.
  3. 3
    Place the reusable functions and variables in that file.
  4. 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

The key difference is intended use: a module is prepared as a reusable source for other programs, while a standalone script is intended to run as its own program.

Worked example

A shared greeting function

Three small programs need the same greeting behavior.

  1. 1
    Identify the repeated functionality

    The greeting behavior is needed in more than one program, so keeping separate copies would duplicate code.

  2. 2
    Place the function in one module

    Create a descriptively named .py file and put the reusable greeting function there.

  3. 3
    Reuse the module

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

  4. 4
    Maintain one source

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

Result

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

Mistakes about reuse

  • Common mistake

    Copying the same function into every program that needs it.

    What it looks like

    Several programs each contain their own version of one shared function.

    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like

    A file is written without considering whether its contents are intended for other programs.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Creating a separate copy of shared functionality for each program.

    What it looks like

    Program A and Program B each maintain independent versions of the same function.

    Why it fails

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

    Do this instead

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

At a glance

  • 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.

Related concepts

More concepts starting with F