Introducing the LEGB Rule
00:01 Whenever you use a name such as a variable or a function name, Python searches through different scope levels to determine whether the name exists or not. To resolve a name, Python follows a specific order of scope levels.
00:30 Here is an example. Python looks up a name in a similar way you look for your sunglasses when you want to go out. The sunglasses may be located on your nose already, or they are enclosed in your bag. Otherwise, they may be somewhere in your apartment, and if you can’t find them, then you can use your built-in eyes and squint them awkwardly to avoid the sun. Hmm, does this example make sense, or do you have a better analogy?
Become a Member to join the conversation.