Search⌘ K
AI Features

Scope

Explore Python’s variable scope rules, focusing on the LEGB model which includes local, enclosed, global, and built-in scopes. Understand how to declare variables with global and nonlocal statements and why managing scope is critical for clean, readable code.

Introduction to scope

The scope of a name is the part of the program in which the name has meaning and can be used. Python’s scope rules are unusual because they follow an “LEGB ...