Understanding the scope of the variables in Python

This article discusses the scope of the variables in Python, which is one of the fundamental concepts of Python programming.

KSV Muralidhar
5 min readMar 28, 2021
Image by Chris Ried on Unsplash

Scope of a variable is the region in the code where the variable is available/accessible. A variable declared outside a function (i.e. the main region of the code) is called a global variable and a variable declared inside a function is called a local variable of that function.

##################
GLOBAL VARIABLES
##################
def x:
################
LOCAL VARIABLE
################

Let’s look at an example to understand it better. In the below example, we declare a variable named ‘global_variable’ in the main section of the code and a variable named ‘local_variable’ inside a function.

global_variable = 1def function():
local_variable = 2
return global_variable
print(f'Global variable call inside function: {function()}')
print(f'Global variable call outside function: {global_variable}')
print(f'Local variable: {local_variable}')
Image by author

The ‘global_variable’ is accessible from anywhere in the code. While, the ‘local_variable’ declared inside a function is only accessible from inside that function. We aren’t able to access it from outside the function. In the next example, we’ll create a variable with the same name both in the local and global scope.

variable = 1def function():
variable = 2
return variable
print(f'variable call inside function: {function()}')
print(f'variable call outside function: {variable}')
Image by author

The ‘variable’ called from inside the function returned 2, while the one called from outside the function returned 1. This is because Python searches for a variable in the local scope first and then in the global scope. Here ‘variable’ declared inside a function is local variable and is available only within the function.

A global variable can be accessed from anywhere in the program. Though we can access a global variable from within a function, we cannot modify it. Let’s see it in the next example.

global_variable = 1def function():
global_variable += 1 #Modifying a global variable
return global_variable
function()
Image by author

We got an error when we tried to increment the ‘global_variable’ from within the function. To avoid this error and modify the global variable from within a function, we need to use the keyword ‘global’. We’ll see it in the next example.

glob_var = 1def function():
global glob_var #Using global keyword

print(f'Global var inside function before increment:{glob_var}')
glob_var += 1

print(f'Global var inside function after increment:{glob_var}')

function()
print(f'Global variable call outside function:{glob_var}')
Image by author

We were able to modify the global variable from within the function after declaring it using the ‘global’ keyword inside the function.

Let’s look at the scope of a variable in case of nested functions. We’ll create a function named ‘fourth_power’ with a nested function named ‘square’. The function ‘fourth_power’ takes an input argument and passes it to the nested function ‘square’, which squares the input argument. Then, the function ‘fourth_power’ returns the square of the output returned by the nested function ‘square’.

def fourth_power(x):
def square(y):
return x ** 2 #x is accessed from fourth_power
return square(x) ** 2
print(f'Fourth power of 3 (3^4) is {fourth_power(3)}')

We can see that the function square takes in an input argument ‘y’ but returns ‘x**2’. But there is no error, this is because, initially Python searched for the variable ‘x’ in local scope of ‘square’, but couldn’t find it. Therefore, it searched for variable ‘x’ in the scope of function ‘fourth_power’ and used it. Hence, the function ‘square’ returns (3 ** 2). the function fourth power returns (3 ** 2 ** 2) which is 81.

Similar to one of our previous examples, we get an error if we try to modify a variable which isn’t declared in the local scope of a function. We’ll see it in the next example.

def fourth_power(x):
def square(y):
x = x + 1 - 1 # Modifying non-local variable
return x ** 2
return square(x) ** 2
print(f'Fourth power of 3 (3^4) is {fourth_power(3)}')
Image by author

We got an error as we tried to modify a variable which isn’t declared in the local scope of the function ‘square’. We can avoid this error using the keyword ‘nonlocal’. Since, the variable ‘x’ is not in the global scope, we can’t use the ‘global’ keyword.

def fourth_power(x):
def square(y):
nonlocal x #Using nonlocal keyword
x = x + 1 - 1
return x ** 2
return square(x) ** 2
print(f'Fourth power of 3 (3^4) is {fourth_power(3)}')

Using the keyword ‘nonlocal’, we could modify the variable which isn’t declared in the local scope of a function. Since, we incremented the variable by 1 and decremented it by 1, the value of the variable remains unchanged.

This brings us to an end of this article where we’ve discussed about the scope of the variables in Python and the usage of ‘global’ and ‘nonlocal’ keywords to modify the variables which aren’t declared in the local scope of a function.

Know more about my work at https://ksvmuralidhar.in/

--

--

KSV Muralidhar
KSV Muralidhar

Written by KSV Muralidhar

Data Science | ML | DL | NLP | CV | Web scraping | Kaggler | Python | SQL | Excel VBA | Tableau | About Me: https://ksvmuralidhar.in/

No responses yet