Nested helpers and remembered values

A tiny helper that only one function needs can live inside it. A nested function that still sees an outer name after the outer call returns is a closure — the idea behind some wrappers later.

Lab recipe cards

Functions are reusable procedures.

You will be able to

  • Park a tiny helper inside the only function that needs it
  • See that the inner function can read an outer name (a closure on a laptop)
  • Lift the helper out when a second caller needs it

A helper that only one function needs can live inside it

gst_bill needs to apply a rate to a base. Nobody else on this file should call that add-on. Nesting def add_on inside gst_bill keeps the name off the module. On a laptop that inner function can still see rate from the outer parameters after you think about closures — the remembered outer names. Here, call the inner function before the outer returns; that is enough to feel the shape.

If the canteen kiosk and the stores batch both need add_on, it is no longer nested. Nested is for privacy of a name, not for showing off indent.

Remembered values are a later tool

A nested function that still sees an outer name after the outer call has returned is a closure. Wrappers, some decorators, and some callback styles use that idea. You do not need to ship a closure to print a GST bill. You do need to know why an inner function can use rate without listing it again.

Do not nest five layers. If the inner body is longer than the outer, the design inverted. Flatten.

Laptop: inner function remembers rate

def make_gst(rate):
    def apply(amount):
        return amount * (100 + rate) / 100
    return apply

gst18 = make_gst(18)
print(gst18(100))

Words that matter

Nested def
A function defined inside another — a private helper.
Enclosing scope
The outer function’s names the inner def can read.
Closure
An inner function that still sees outer names after the outer call returns.

Common mistakes

Avoid: Nesting a helper that three other files also need.

Do this: Put shared helpers at file level; nest only a private one-off.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — inner add-on

rate is an outer parameter. Inner function uses it. 100 at 18 is 118.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — inner PF cut, outer print-shaped return

Inner helper only exists during net_pay. 100 minus 12 is 88.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — Late fee bands as an inner chooser

The outer function is the public name. The inner if is not imported elsewhere.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Nest the GST add-on

Inside gst_bill, define a nested helper that applies 18 percent. Print gst_bill(100) so 118 appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. Nest a function when…
2. A closure is…

Progress is stored in a browser cookie on this device.