liveBook · Manning

LEGB

LEGB is Python’s name-resolution order of local, enclosing-function, global, and built-in scopes.

How 3 Manning books explain it

Python How-To

The LEGB rule is Python's name-resolution rule that searches local, enclosing-function, global, and built-in scopes in that order.

How it is used
Explains how Python resolves names and helps diagnose attempts to change variables across scopes.
How deep it goes
Defines the lookup sequence and works through local versus global assignment, including the global declaration.
Examples
Uses a database filename setter that first leaves a global unchanged, then updates it with a global declaration.
What you can do after
Diagnose/debug cross-scope name resolution and identify when global or nonlocal is needed for an assignment.

Read how Python How-To explains LEGB →

Python Workout, Second Edition

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

How it is used
Explains how Python resolves names inside and outside functions.
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 you can do after
Diagnose name-resolution surprises and choose local, global, or nonlocal bindings deliberately.

Read how Python Workout, Second Edition explains LEGB →

Python Workout: 50 ten-minute exercises

LEGB is Python's name-resolution order of local, enclosing-function, global, and built-in scopes.

How it is used
Explains Python name visibility, shadowing, nested-function behavior, and closure state.
How deep it goes
Defines lookup order and demonstrates closures and shadowing, while stopping short of a broader namespace implementation treatment.
Examples
Uses shadowed sum, inner-versus-outer variables, password generators, and functions retaining enclosing values.
What you can do after
Diagnose name-resolution errors, predict variable visibility, and design functions that retain enclosing state.

Read how Python Workout: 50 ten-minute exercises explains LEGB →

What to know first

Related concepts

Questions about LEGB

What is LEGB?

LEGB is Python’s name-resolution order of local, enclosing-function, global, and built-in scopes.

Which Manning books explain LEGB?

Python How-To, Python Workout, Second Edition and Python Workout: 50 ten-minute exercises.

What should I know before learning LEGB?

Manning books that teach LEGB usually introduce namespace, function, object and password authentication first.

What is the difference between LEGB and ICPO?

LEGB is Python’s name-resolution order of local, enclosing-function, global, and built-in scopes. ICPO is a mnemonic for Python attribute lookup order across an instance, its class, parent classes, and the object hierarchy.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.