Import Reusable Code

Move parsing and calculation into a module, then import its names without hiding where they came from or starting a workflow by surprise.

The measurement runner now has several jobs: locate a file, read its lines, parse the rows, compute a summary, and save a report. Parsing and computation do not need to know where the files live. Moving those functions into a module lets us test and reuse them separately.

Give Reusable Functions Their Own Module

Every Python file can act as a module. Create measurement_tools.py for the operations that transform values:

The module defines functions. It does not open data/measurements.txt, create reports/, or write a report when imported. Those actions belong to the runner because they describe one complete workflow.

The runner can import the module and use its namespace:

The prefix measurement_tools. shows where each imported name comes from. It also prevents a local function named mean from being confused with the module's function.

Q1. Call through a module namespace

After import measurement_tools, which expression calls the parser defined in that module?

Choose one

Select one choice, then check.

HintUse the imported name

import measurement_tools binds the name measurement_tools, not each function as a separate local name.

SolutionQualify the function name

Call measurement_tools.parse_measurements(numbered_lines). The .py filename suffix is not part of the expression.

Not attempted
Review

Not marked done.

Import Only the Names You Intend to Use

A selective import brings named functions directly into the current file:

Both forms are ordinary. Importing the module keeps its origin visible at each call. A selective import is concise when a file uses a few specific names and their origin remains clear.

Avoid wildcard imports such as:

from measurement_tools import *

The star does not show which names enter the file. It becomes harder to tell whether mean was defined locally or imported, and a later module change can introduce another name unexpectedly. Write the required names explicitly.

Imported code also runs top-level statements while the module is loaded. A reusable module should therefore avoid surprising work such as reading input or writing reports merely because another file imports it. Function bodies run when called, not when defined, so definitions are safe module-level work.

Q2. Replace a wildcard import

The runner uses only parse_measurements and mean. Which import states that dependency explicitly?

Choose one

Select one choice, then check.

HintMake dependencies visible

The runner needs two known functions, not every public name in the module.

SolutionName both imported functions

Use from measurement_tools import mean, parse_measurements. Readers can then see the runner's imported dependencies without inspecting a wildcard.

Not attempted
Review

Not marked done.

Know Where a Module Comes From

Imports can refer to three common origins:

OriginExampleAvailability
local projectmeasurement_toolsa file supplied by this program
standard librarypathlib or statisticsincluded with Python
third partynumpyinstalled separately when local work needs it

The origin affects how the code is supplied, but the import syntax still binds names. Importing does not install a missing third-party package.

Some libraries have widely used aliases. Later numerical code commonly uses:

import numpy as np

An alias should follow a recognizable convention rather than invent a short name for every module. For our small local module, measurement_tools is already clear.

The standard-library statistics module provides a compact namespace example:

Q3. Use a standard-library namespace

Complete the code using import statistics and call mean through that module namespace.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintKeep the module name at the call

Add import statistics, then assign result = statistics.mean(readings).

SolutionImport and use the namespace
Not attempted
Review

Not marked done.

A module gives reusable functions a clear home. Import the module namespace or name a few dependencies explicitly, avoid wildcard imports and surprising import-time work, and distinguish local, standard-library, and third-party origins. The next lesson builds on known manual loops with two standard-library collection tools.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.