Python Scope
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
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 variablex
in theglobal scope
line 3
: we create a function calledlocal_scope_example()
line 7
: we calllocal_scope_example
- Whenever we call a function, we create a new
execution context
or newscope block
— So we are now back toline 4
line 4
: we are in a brand new execution context and create an integer and assignx
to the reference of that integer, we currently have no idea there is also a globalx
. This is also referred to asshadowing
.line 5
: We call theprint
function, python will look within its current execution context to find the variable that is bound within thelocal_scope_example
, it finds x so it prints100
. It’s very important to remember if there was no variable namedx
bound withinlocal_scope_example
python would have jumped to the nextScope
which in this case isGlobal
.Line 8:
We jump back out toGlobal
Scope and call theprint
function again. Again Python will look in its own execution context first and then jump outside if it needs to. Python finds thex
which is located online 1
and prints100
. It’s important to remember here that python always goes inside out, so it never jumps insidelocal_scope_example
and thus never knowsx
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.