What is LEGB in Python??

What is LEGB in Python??

·

0 min read

WhatsApp Image 2020-05-31 at 20.33.54.jpeg

Recently I came across the term LEGB in python.

I first thought it to be some lingo for a python library, python libraries do have weird names. you have to agree with me on that point.

But, the term LEGB deals with scope resolution in python.

Scope defines the accessibility of the python object

The region of the code where the variable is visible is called the scope of that variable. Scope set rules for variables, like how they can be accessed when to from where.

Why scope matters?

For that, we have to understand namespaces

Every python object holds 3 things...

  1. Object type
  2. Object value
  3. Object reference counter
a = 2
a =a+1
print(id(2)) 

print(id(a))

id(2) = 9756224
id(a) = 9756256

2 is an object stored in memory and a is the name we associate it with.

Here the name is assigned to value, so python doesn't have to create a duplicate object and saves memory.

Python implements namespaces in the form of dictionaries. It maintains a name-to-object mapping where names act as keys and the objects as values. Multiple namespaces may have the same name but point to a different variable.

{a : {2,3}}

Namespaces help us to reduce conflict of using the same variable name in our program but, all namespaces used in python have boundaries where they behave differently.

Suppose if you have like the above

a = 2

a = a+1

The identifier variable will point to object of value 3 only to again go to fetch value 2 you will have again to re-assign to value 2 increasing lines of codes and redundancy.

So, what exactly LEGB stands for??

L: Local

E: Enclosed

G: global

B: built-in

The python interpreter finds the variable from inside to outside fashion.

I.e. it goes from L←E←G←B

Local Scope is defined inside the function body, it is defined as the innermost scope.

a_level_3 = 5
b_level_3= 7

def outer():
    global a_var
    a_level_2 = 3
    b_level_2 = 9
    def inner():
        global a_var
        a_level_1 = 4
        b_level_1 = 8
        print('a_var inside inner_foo :', a_var)
        print('b_var inside inner_foo :', b_var)
    inner()
    print('a_var inside outer_foo :', a_var)
    print('b_var inside outer_foo :', b_var)

outer()
print('a_var outside all functions :', a_var)
print('b_var outside all functions :', b_var)
Here goes the output of the above code after execution.

a_level_1 inside inner : 4
b_level_1 inside inner : 8
a_level_2 inside outer : 4
b_level_2 inside outer : 9
a_level_3 outside all functions : 4
b_level_3 outside all functions : 7

Python interpreter searches from level 1 and goes to level 3.

If you look at level_2 "a" variable it is still 4, whereas b is 9.

We can simply import global into the function so, the value of variable what is defined inside that function.

But variable a is defined global at level defined inside that level 2 and level 1 but preference is giving to level 1 as it was encountered first by the python interpreter.

Enclosing Scope

Neither a local or global variable is called an enclosing Scope.

Defined inside nested functions or loops.

We can create a non-local variable using a non-local word.

'nonlocal' gives read & 'write' permission for that specific variable of the outer parent function.

Non-local can be used inside inner functions only.

Without non-local word

x = 0
def outer():
    x = 1
    def inner():
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)

outer()
print("global:", x)

# inner: 2
# outer: 1
# global: 0

With non-local word

x = 0
def outer():
    x = 1
    def inner():
        nonlocal x
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)

outer()
print("global:", x)

# inner: 2
# outer: 2
# global: 0

Built-in Scope.

If a variable is not local-global or enclosed it is called a built-in scope.

from math module pi is imported and the value of pi is not defined in global, local, and enclosed. Python then looks for the pi value in the built-in scope and prints the value.

This is one of the widest scopes.

Print(),Id() also comes under built-in scopes.

There is too much in-depth to learn about the internals, but that won't stop us from learning.