Image

Real Python: The LEGB Rule & Understanding Python Scope

The concept ofscoperules howvariablesand names are looked up in your code. It determines the visibility of a variable within the code. The scope of a name or variable depends on the place in your code where you create that variable. The Python scope concept is generally presented using a rule known as theLEGB rule.

The letters in the acronym LEGB stand forLocal, Enclosing, Global, and Built-inscopes. This summarizes not only the Python scope levels but also the sequence of steps that Python follows when resolving names in a program.

In this video course, you’ll learn:

  • Whatscopesare and how they work in Python
  • Why it’s important to know aboutPython scope
  • What theLEGB ruleis and how Python uses it to resolve names
  • How to modify thestandard behaviorof Python scope usingglobalandnonlocal
  • Whatscope-related toolsPython offers and how you can use them

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days.>> Click here to learn more and see examples]

https://realpython.com/courses/legb-rule-python-scope/