Python Scope

Connor Littleton
3 min readJun 8, 2020
Photo by Chris Ried on Unsplash

In Computer Programming, the scope of a name binding — an association of a name to an entity, such as a variable — is the region of a computer program where the binding is valid:where the name can be used to refer to the entity. Such a region is referred to as a scope block.

So, the above definition may be a little hard to depict, so lets examine what that means in reference to python and its own Scope

There are four Scopes in Python:

1. Local — Inside the current function
2. Enclosing — Inside enclosing functions
3. Global — At the top level of the module
4. Build In — the special builtins module

Local Scope

This is the code block or body of any Python Function

In the example above, we can immediately see how scopes work in a bubble up fashion.

These are the questions you should probably be asking yourself right now:
1. How is
local_scope_example() returning 100 ?
2. How is
print(x) returning 50 ?

Lets break down each line step by step:

  • line 1 : We declare a variable x in the global scope
  • line 3 : we create a function called local_scope_example()
  • line 7 : we call local_scope_example
  • Whenever we call a function, we create a new execution context or new scope block — So we are now back to line 4
  • line 4 : we are in a brand new execution context and create an integer and assign x to the reference of that integer, we currently have no idea there is also a global x . This is also referred to as shadowing .
  • line 5 : We call the print function, python will look within its current execution context to find the variable that is bound within the local_scope_example , it finds x so it prints 100 . It’s very important to remember if there was no variable named x bound within local_scope_example python would have jumped to the next Scope which in this case is Global .
  • Line 8: We jump back out to Global Scope and call the print function again. Again Python will look in its own execution context first and then jump outside if it needs to. Python finds the x which is located on line 1 and prints 100 . It’s important to remember here that python always goes inside out, so it never jumps inside local_scope_example and thus never knows x was over written.

Phew! That was tedious but also crucial in understanding how Scope Binding works in Python. We can apply this same exact knowledge to every other Scope

Enclosing Scope

This is a special scope that only exists for nested functions. The enclosing scope is the scope of the outer or enclosing function.

Global Scope

This is the top most scope. Names in this scope are visible from everywhere in your code.

Built-in Scope

This scope contains names such as keywords, functions, exceptions, and other attributes that are built into Python.

Conclusion

We have gone over the four scopes in python, Local, Enclosing, Global, Built-in. We also discussed how each scope checks itself to see if it can satisfy the requirements it needs and if it cannot then it checks the scope above it and continues in this fashion until the variable is found. If the variable is never found, an error is thrown.

I hope This has been helpful for you in understanding how scopes work! Happy Coding.

--

--