LEGB — Variable Scope in Python

Local, Enclose, Global and Build-in

Allie Hsu
CodeX
Published in
5 min readJul 25, 2021

--

Photo by James Lee on Unsplash

Each programming language has its own variable scope. For python, we could simply follow the LEGB rule that includes

Local, Enclose, Global, and Built-in.

In this article, we will use a few examples to understand their feature and how to access objects of different scopes.

--

--

Allie Hsu
CodeX
Writer for

A tech enthusiast who is keen to develop new skills