liveBook · Manning

LEGB

As explained in Python Workout, Second Edition

The LEGB rule is Python's variable-lookup order of local, enclosing, global, and built-in scopes.

LEGB describes Python’s variable-lookup order: local scope, enclosing function scopes, global scope, then built-ins. Python stops at the first matching name. Outside functions, lookup uses globals before built-ins; indentation alone does not create a new scope. section 7.5

Use the lookup order to reason about common scope behavior:

  • A function-local assignment can hide a global with the same name without changing the global value. section 7.5
  • A global name can shadow a built-in: assigning sum = 0 makes sum([10, 20, 30]) fail because Python finds the integer first. section 7.5
  • An inner function can read a value from its enclosing function, and each outer-function call can preserve a separate value through a closure. section 7.11
  • Use global to rebind a module-level name from a function, or nonlocal to update a name in an enclosing function. section 7.5

A closure with independent state

def foo(): call_counter = 0 def bar(y): nonlocal call_counter call_counter += 1 return f'y = {y}, call_counter = {call_counter}' return bar b = foo() print(b(10)) # y = 10, call_counter = 1

sum = 0
for i in range(5):
    sum += i
print(sum)

print(sum([10, 20, 30]))

TypeError: 'int' object is not callable

Inspect the built-in-shadowing failure caused by assigning an integer to sum. section 7.5

How Python Workout, Second Edition covers it

How deep it goes
Defines four lookup levels and works through shadowing, global rebinding, closures, and nonlocal state.
Examples
Uses nested functions, password generators, inner and outer x values, sum shadowing, and a nonlocal call counter.
What it assumes
Assumes familiarity with functions and nested functions while clarifying variable visibility from first principles.
What you can do after
Diagnose name-resolution surprises and choose local, global, or nonlocal bindings deliberately.
How it connects
Connects local scope, enclosing scope, global scope, built-in namespace, closures, shadowing, global, and nonlocal.

Also explained in

Compare how 3 books explain LEGB →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.