1. Concept library
  2. Python Foundations
  3. Module 3: Functions

Concept

Scope

Where a name is visible: inside the function that assigned it, or at module level for everything. Python looks up names local first, then enclosing functions, then global, then built-ins.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain the order Python looks up a name: local, enclosing, global, built-in
  2. Predict what a function prints when it reads a global and when it assigns to one
  3. Spot the UnboundLocalError caused by assigning to a global name without global

In practice

A few lines that show Scope doing its job.

Python
counter = 0

def bump():
    global counter
    counter += 1

def describe():
    label = "local"     # only visible here
    return label

bump()
print(counter, describe())   # 1 local

The common mistake

Watch for

Assigning to a module-level name inside a function without global. The assignment creates a new local variable, and reading it earlier in the same function raises UnboundLocalError.

What to know first

Your roadmap puts these before Scope, unless you already know them.

How it stays learned

Once a session shows you understand Scope, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Default and keyword arguments