Pages

Thursday 23 March 2023

Understanding Variable Scope in Python

 Introduction


In Python, variable scope determines the visibility of a variable within a program. Python has several types of scopes, including global scope, local scope, and nonlocal scope. Understanding the different types of scopes and how they work is essential for writing efficient and effective Python code. In this blog post, we'll explore Python scope and learn how to use it to write better code.


Global Scope

A global variable is a variable that is defined outside a function. Global variables are accessible from anywhere in the program, including inside functions.


Example of Global Scope

Let's take an example to understand global scope better.


x = 10

def my_function():

print("The value of x in my_function is:", x)


my_function()

print("The value of x outside my_function is:", x)

Output:

The value of x in my_function is: 10

The value of x outside my_function is: 10

In this example, we have defined a global variable x outside the my_function(). The my_function() is defined to print the value of x.


Inside the my_function(), we are printing the value of x. Since x is a global variable, it is accessible from anywhere in the program, including inside the function.


Outside the function, we are also printing the value of x. Since x is a global variable, it is accessible from anywhere in the program, including outside the function.


However, if we try to modify the value of x inside the function, we need to use the global keyword.


x = 10

def my_function():

    global x

    x = 20

    print("The value of x in my_function is:", x)

my_function()

print("The value of x outside my_function is:", x)

Output:

The value of x in my_function is: 20

The value of x outside my_function is: 20

In this example, we have used the global keyword inside the my_function() to modify the value of x. Since x is a global variable, it is accessible from anywhere in the program, including inside the function. But to modify the value of x, we need to use the global keyword.


After modifying the value of x, we are printing the value of x both inside and outside the function. The output shows that the value of x is modified to 20.


Local Scope


A local variable is a variable that is defined within a function. Local variables are accessible only within the function in which they are defined. They cannot be accessed from outside the function.


Example of Local Scope


Let's take an example to understand local scope better.


def my_function():

    x = 10

    print("The value of x in my_function is:", x)

my_function()

Output:

The value of x in my_function is: 10

In this example, we have defined a function my_function(). The x variable is defined within the my_function(). The x variable is a local variable because it is defined within the function.


In the my_function(), we are printing the value of x. Since x is defined within the function, it is accessible only within the function. Therefore, we are able to print the value of x within the function.


However, if we try to print the value of x outside the function, we will get an error.


def my_function():

    x = 10

my_function()

print("The value of x outside my_function is:", x)

Output:

NameError: name 'x' is not defined

In this example, we have defined the x variable within the my_function(). But we are trying to print the value of x outside the function. Since x is a local variable, it is not accessible outside the function. Therefore, we get a NameError.


Nonlocal Scope


In Python, nonlocal variables are used in nested functions, which are functions defined inside other functions. A nonlocal variable is a variable that is not local to the current function, but is also not global. Nonlocal variables are used to modify variables in the outer function from an inner function.


The nonlocal keyword is used to declare a variable as nonlocal. When a variable is declared as nonlocal, it means that it is not local to the inner function, but is local to the outer function.


Example of Nonlocal Scope

Let's take an example to understand nonlocal scope better.


def outer_function():

    x = 10

    def inner_function():

        nonlocal x

        x = 20

    inner_function()

    print("The value of x in outer_function is:", x)


outer_function()

Output:

The value of x in outer_function is: 20

In this example, we have defined two functions - outer_function() and inner_function(). The x variable is defined in the outer_function(). The inner_function() is defined inside the outer_function().


The nonlocal keyword is used to declare the x variable as nonlocal in the inner_function(). This means that x is not local to the inner_function(), but is local to the outer_function().


In the inner_function(), we are modifying the value of x to 20. Since x is declared as nonlocal, it is modifying the value of x in the outer_function().


Finally, we are printing the value of x in the outer_function(). The output shows that the value of x in the outer_function() is 20, which was modified by the inner_function()

Conclusion

Scope is a fundamental concept in Python that determines the accessibility and visibility of variables and functions in a program. Understanding scope is essential for writing efficient and bug-free code. In Python, variables and functions have different scopes depending on where they are defined, but scope is not a data type.

Please subscribe my youtube channel for latest python tutorials and this article

No comments:

Post a Comment