Scopes, Closures and Decorators · Lecture 01 · 12 min

Global and Local Scopes

Where a name is assigned decides where it lives. Python looks names up local first, then enclosing, global and built-in.

Python loads when you run a cell Shift+Enter runs a cell. Cells share variables, like a notebook.

Names, namespaces and scopes

When you write a = 10, Python binds the name a to an int object. That binding has to be stored somewhere, and it is: in a namespace, which you can picture as a table (in practice, usually a dictionary) mapping names to objects.

A name binding is not visible everywhere in your program. The region of code where a name can be used to reach its object is called the name’s scope. In Python, scope is lexical: it’s determined by where in the source code the name is assigned, not by the order in which functions happen to be called.

Every scope has its own namespace. The three you meet first are:

  • Built-in scope: names like print, len, True, None, dict, ValueError. They’re available everywhere, in every module.
  • Global scope: the top level of a module (one .py file, or here, the page you’re reading). Despite the name, there’s no scope that spans all modules of a program; “global” in Python always means module-level.
  • Local scope: created each time a function is called. Parameters and any names assigned inside the function body live here, and the namespace disappears when the call ends.
3Built-in scope print, len, range, True, None … 2Global (module) scope a = 10, my_func, other_func 1Local: my_func n, c a: found in 2 print: found in 3 Local: other_func x can't see n or c: siblings are separate Lookup from my_func: 1→2→3→ NameError
Scopes nest. A name is looked up in the current scope first, then each enclosing scope outward to the built-ins. Lookup never goes inward or sideways.

Looking a name up

When Python needs the object behind a name, it searches the namespaces from the inside out: Local, then Enclosing functions (next lesson), then Global, then Built-in. This is often remembered as LEGB. If the name isn’t found anywhere, you get a NameError.

a = 10

def my_func(n):
    print('global a:', a)   # not local, so Python finds it in the module
    c = a ** n              # c and n are local
    return c

my_func(2)

Inside my_func, n and c are local. a is never assigned inside the function, so it’s looked up in the enclosing (global) scope. print isn’t local or global either, so Python finds it in the built-in scope.

Assignment makes a name local

Here’s the crucial rule: if a function assigns to a name anywhere in its body, that name is local to the function, for the whole function, unless you say otherwise. A local name can mask (hide) a global with the same name:

a = 10

def my_func(n):
    a = 2          # a brand-new local 'a', unrelated to the global one
    return a ** n

print(my_func(3))
print('global a is still', a)

The global keyword

To re-bind a module-level name from inside a function, declare it with global:

a = 10

def my_func(n):
    global a       # 'a' in this function means the module's 'a'
    a = 2
    return a ** n

print(my_func(3))
print('global a is now', a)

global can even create a module-level variable. The variable doesn’t exist until the function actually runs:

def make_var():
    global var
    var = 'hello world'

print('var' in globals())   # globals() returns the module's namespace dict
make_var()
print(var)
Use `global` sparingly

A function that silently changes module state is hard to test and hard to reason about. Most of the time it’s better to return a value and let the caller decide what to do with it. You’ll rarely need global outside of small scripts.

Scope is decided at compile time

Python works out which names are local when it compiles the function, before any line of it runs. It doesn’t matter where in the body the assignment is, or whether that line ever executes. This leads to one of the most common surprises in Python:

a = 10
b = 100

def my_func():
    print(a)
    print(b)     # b is local here, because of the assignment below
    b = 1000

try:
    my_func()
except UnboundLocalError as ex:
    print('UnboundLocalError:', ex)

The first print works: a is never assigned in my_func, so it’s global. But b = 1000 made b local to the entire function, so the earlier print(b) tries to read a local variable that hasn’t been given a value yet. Python does not fall back to the global b.

You can see the compiler’s decision on the function’s code object. co_varnames lists the local variable names:

my_func.__code__.co_varnames
Clearer error message

UnboundLocalError is a subclass of NameError. Since Python 3.11 its message reads “cannot access local variable ‘b’ where it is not associated with a value”. Older versions (and older notebooks) say “local variable ‘b’ referenced before assignment”. Same error, friendlier wording.

Built-ins can be masked too

Functions are objects bound to names like any other, so the scoping rules apply to them. If you assign to print at module level, your module-level print masks the built-in one:

print = lambda x: f'hello {x}!'

def greet(name):
    return print(name)      # LEGB finds the module-level 'print' first

greet('world')

The real print hasn’t gone anywhere; it still lives in the built-in namespace. Deleting the global name unmasks it:

del print
print('the built-in print is back')

This is exactly why you should avoid naming your own variables list, dict, str, id, max and so on. It works, until the moment you need the built-in.

Loops and if blocks don’t create scopes

If you come from Java, C# or C++, you might expect a variable assigned inside a loop to disappear after the loop. In Python, only modules, functions (including lambdas), classes and comprehensions create scopes. A for, while, if or with block does not:

for i in range(10):
    x = 2 * i

print(i, x)    # both are ordinary module-level names

Comprehensions are the exception you should know about: their loop variable stays inside the comprehension.

squares = [k * k for k in range(5)]
print(squares)
print('k' in globals())
Comprehensions in Python 3.12+

Since 3.12 (PEP 709) list, set and dict comprehensions are inlined into the surrounding code instead of running as a hidden nested function, which makes them faster. The scoping behavior you see is unchanged: the loop variable still doesn’t leak.

Key takeaways

  • A namespace maps names to objects; a scope is the region of code where a binding is visible. Python’s scopes are lexical.
  • “Global” means module level. Built-ins sit in an outer scope available everywhere.
  • Name lookup goes Local → Enclosing → Global → Built-in.
  • Any assignment in a function body makes that name local for the whole function, decided at compile time. Reading it before assigning raises UnboundLocalError.
  • global name lets a function re-bind (or create) a module-level name.
  • Loops and if blocks don’t create scopes; functions, classes, modules and comprehensions do.