AI engineering glossary · Python core

What is LEGB rule?

LEGB rule: The order in which Python searches for a name: Local scope of the current function, Enclosing functions, Global scope of the module, then Built-in names. The first match found is used.

Level: intermediateTopic: Python core

LEGB rule: an example

If x = 1 is global and a function also sets x = 2, code in the function sees 2 while the module still sees 1.

Related terms

More Python core terms

Where you learn it

LEGB rule is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.