Advanced

Names live in a region

A name is only alive inside the region where it was made. Outside that region the same letters mean nothing, or mean something else. Scope is the rule that keeps two parts of a program from stepping on each other.

What you will see

3

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Prove that a name stops at its region.

1

Make a function with its own name inside it.

def make(): total = 5 return total

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
2

Call it. The value comes out.

print(make())

Before you run it, say out loud what it will print. Then open the answer.

What it prints
5
3

Try to read the name from outside. It is gone.

print(total)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
NameError
The name never left the region. To move a value out you return it, and to move it in you pass an argument. Names themselves never travel.
4

Two functions using the same letters, safely.

def a(): x = 1 return x def b(): x = 2 return x print(a() + b())

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A call makes a fresh region, names made inside live only there, and only the returned value leaves.
1 make() starts a region
↓
2 total is made inside
↓
3 return hands the value out
↓
4 the name dies at the end
Why this works
Why regions exist

Without regions, every name in a program would be one shared pool, and a function that used a common letter would break anything else that used it. Regions make each call independent, which is what lets a program be built from pieces at all.

Reading a program by scope

Find where a name was made, and that region is the only place it can be read. If a name is not in the region you are looking at, it belongs to an outer one or it is a bug. Following this rule is how you find the value a line really uses.

Examples

Inside a function

def make():
    total = 5
    return total

print(make())
print(total)

The first print works, the second fails. total was made inside make and died when make finished. The value came out, the name did not.

Same name, different places

def a():
    x = 1
    return x

def b():
    x = 2
    return x

print(a() + b())

Both functions use x and neither sees the other. Each call makes a fresh region, so the two x values never meet.

Arguments are local

def twice(n):
    return n * 2

n = 100
print(twice(4))
print(n)

The argument n belongs to the call, not to the n outside. The print shows 8 then 100, which proves the two names are separate despite sharing letters.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A name can hold nothing
Previous Lesson
A name can hold nothing
Next Lesson
Names and values are separate things
Names and values are separate things →