Scope in Python

python scope overview
10 November 2024

In the Python programming language, the concept of Scope refers to one of the key and important points in managing access to variables. In summary, Scope represents the range of code in which a variable can be used and accessed. A correct and logical understanding of Scope can help you in managing specific Python projects and developing more efficient code.

Python has several different types of Scope, which include: Local, Enclosing, Global, and Built-in. These types are structured in priority order and based on the LEGB (Local, Enclosing, Global, Built-in) algorithm. For instance, Local is the innermost level defined within a function, and in contrast, Built-in, which is related to Python itself, is considered at the highest level.

Variables defined inside a function can only be accessed within that function, and they have what is called Local Scope. On the other hand, if a variable is defined at the main level of a Python program, that variable will have Global Scope and be accessible throughout the entire program unless a variable with the same name is defined in Local Scope.

To create and properly use variables in Python, it is essential to understand the differences between these levels and their behaviors during execution. This understanding can help prevent the creation of unintentional global variables in your program and improve code maintainability.

Additionally, by using keywords like global and nonlocal, you can gain more control over the Scope of your variables. For example, if you need to change a variable within a function that is defined in Global Scope, you would use global to inform Python that this variable should be treated as a global one.

In essence, a proper understanding of this concept can help you progress in Python and lead to more efficient, streamlined code. Below, I will provide an example of code along with an explanation of the lines:


    def outer_function():
x = 'local variable in outer'
def inner_function():
nonlocal x
x = 'modified by inner'
print('Inner:', x)
inner_function()
print('Outer:', x)
outer_function()

def outer_function():
This line defines the function outer_function, which contains a local variable x.

x = 'local variable in outer'
The variable x is defined in the outer_function and is initially assigned a value.

def inner_function():
The function inner_function is defined within outer_function, meaning it has Enclosing Scope.

nonlocal x
This line indicates that we are referring to the variable x from the Enclosing Scope, namely the one in outer_function.

x = 'modified by inner'
Here, the value of x in the Enclosing Scope is modified.

print('Inner:', x)
This will print the value of x after it has been modified in inner_function.

inner_function()
Calls the function inner_function.

print('Outer:', x)
This will print the value of x after executing inner_function.

outer_function()
Calls the function outer_function to execute the entire process.

FAQ

?

Why do variables throw errors in Python?

?

How can I modify Global variables?

?

Can I access parent function variables in Python?