liveBook · Manning

Function

As explained in Get Programming: Learn to code with Python

A helper function is a supporting function that performs a focused subtask for another function or larger program.

A helper function does one focused job for another function. Use one when a task has several parts that are easier to write and check separately. For example, analyzing friends’ phone numbers involves finding unique area codes and then looking up their states. section 23.5 section 23.6

Start by deciding what each helper needs and what it gives back. `get_unique_area_codes` examines phone numbers and returns a tuple without repeated area codes. `get_states` takes those codes and returns their corresponding states. The larger `analyze_friends` function uses the results to print how many friends there are and where they live; it does not return a value. section 23.5

Listing 23.5. Helper function to keep only unique area codes
def get_unique_area_codes():
    """

    Returns a tuple of all unique area codes in phones
    """

    area_codes = ()                                 #1
    for ph in phones:                               #2
        if ph[0:3] not in area_codes:               #3
            area_codes += (ph[0:3],)                #4

    return area_codes

Follow the loop: it takes the first three digits of each phone number and adds that area code to the result only if it is not already there. section 23.5

These helpers are defined inside `analyze_friends` because they serve that particular task. A nested helper can use the enclosing function’s parameters: `get_unique_area_codes` can read `phones` without receiving it again. By contrast, `get_states` receives the particular tuple of area codes it should look up. section 23.5 section 22.1

From phone numbers to states

Suppose four friends’ phone numbers have area codes 801, 609, 206, and 609. The first helper keeps 609 only once; the second looks up the states for the remaining codes. The final printout reports four friends but only three states. If a code is missing from the lookup data, `get_states` supplies `"BAD AREACODE"` for it. section 23.5

Write and test each function soon after creating it, then check that the functions work together. Aim for a self-contained task that can be reused: a function that reads one file, for instance, can be called for several files instead of being written for exactly two. section 23.6 section 29.1 section 29.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Works through nested helpers, their inputs, returned tuples, enclosing scope, and integration with a printing function.
Examples
Uses friends’ phone numbers to find unique area codes, look up states, and report the results.
What it assumes
Builds on function definitions, parameters, returns, and scope while developing the helper-function pattern through an example.
What you can do after
Build it from scratch: split an analysis into helpers, test them individually, and combine them in a larger function.
How it connects
Links helper functions to modularity, nested functions, scope, tuples, and reusable file processing.

Also explained in

Compare how 20 books explain function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.