avatarYang Zhou

Summary

The context explains the LEGB rule in Python, which defines the order in which Python looks up its variables, and the usage of global and nonlocal keywords to modify variables in different scopes.

Abstract

Python's LEGB rule defines the order in which the Python interpreter retrieves a variable's name, including local, enclosed, global, and built-in variables. The context covers the four types of variables, the LEGB rule, and how to use the global and nonlocal keywords to modify variables in different scopes. Examples are provided to illustrate the concepts.

Bullet points

  • The LEGB rule defines the order in which Python looks up its variables: local, enclosed, global, and built-in.
  • Local variables are defined within a function.
  • Enclosed variables are used in the context of nested functions.
  • Global variables are the uppermost level variables.
  • Built-in variables are variables in Python built-in modules.
  • The global keyword is used to modify a global variable within a function.
  • The nonlocal keyword is used to modify a nonlocal or enclosing variable within an inner function.
  • Examples are provided to illustrate the usage of global and nonlocal keywords.

Master Python Scope by the LEGB Rule

Photo by Angela Compagnone on Unsplash

Introduction

The variables scope (or namespace) is a very fundamental concept in programming languages. Every developer, no matter which language he or she is using, knows the definition of local and global variables. However, things become a little complicated in Python. Following questions appeared lots of times in interviews for Python developers:

  • Why Python needs the nonlocal keywords?
  • What is the LEGB rule?
  • Differences between global and nonlocal variables in Python?
  • How many types of variables in Python?

This post will explain the Python scope from elementary to profound. After reading, you will totally master this significant concept.

Four Types of Variables and the LEGB Rule

The LEGB rule defines an order in which the Python interpreter retrieves a variable’s name. The four letters represent four types of variables in Python:

  • Local Variables (L): Variables in a function
  • Enclosed Variables (E): In the context of nested functions (explain later)
  • Global Variables (G): The uppermost level variables
  • Built-in Variables (B): Variables in Python built-in modules
Scope of 4 Types of Variables (Credit: Yang Zhou)

When a name is called in a program, the correspond variable will be retrieved sequentially in the local, enclosing, global, and built-in scope. If the naming variable exists, then the first occurrence of it will be used. Otherwise, an error will be raised.

Let’s see an example:

As the above example shown, the function print_name() called the local variable name. The function print() called the global variable name.

Master the Global Keyword

If we want to modify the global name variable in the function, a statement with the global keyword is needed.

Master the Nonlocal Keyword

The concept of enclosing variables are mentioned in the context of nested functions. For inner functions, the variables of outer functions are neither local variables nor global variables. Here comes the nonlocal or enclosing variables. For example:

As shown above, three different names represent three different types of variables and they are called by the LEGB rule.

If we would like to change a nonlocal variable’s value in the inner function, a nonlocal keyword is needed.

The statement is as simple as global keyword statement.

Conclusion

The LEGB rule defines the order in which Python looks up its variables. If we want to change a global variable in a function, a global statement is needed. If we want to change a nonlocal or enclosing variable in an inner function, a nonlocal statement is needed.

Thanks for reading! More great Python tutorials are here:

Programming
Technology
Python
Software Development
Software Engineering
Recommended from ReadMedium