DEV Community

Cover image for Exploring Variable Scope in Python with Nested Functions
Sanjay Khati Chhetri
Sanjay Khati Chhetri

Posted on

Exploring Variable Scope in Python with Nested Functions

Introduction:

Understanding variable scope is key to writing clean, efficient Python code. As variables can have different scopes, knowing how and when to access them is crucial. In this post, we will discuss variable scope in Python using an illustrative example involving nested functions. We will explore how variables in global, enclosed, and local scopes are accessed by different functions.

Example Python Code:

Below is the code snippet we will use to demonstrate variable scope.

global_var = 10

def enclosed_fn(enclosed_var):
    enclosed_var = 8
    print("i am inside the enclosed function, i can access variables in global scope:",global_var,"and enclosed scope:",enclosed_var,"\n")

    def local_fn():
        local_var = 5
        print("i am inside the local function, i can access variables in global scope:",global_var,"enclosed scope:",enclosed_var,"and local scope:",local_var,"\n")
    local_fn()

enclosed_fn()

print("i am a example of global function called print() , i can access variable declared outside the function or in global scope:",global_var,"\n")
Enter fullscreen mode Exit fullscreen mode

Understanding the Code:

In this code, we define a global variable global_var, an enclosed function enclosed_fn, and a local function local_fn nested within enclosed_fn. Each function prints a statement demonstrating its ability to access variables from different scopes.

  1. Global Scope:
    Variables declared outside of any function or block are considered global variables. Their lifecycle extends throughout the entire runtime of the program. In our example, global_var is a global variable.

  2. Enclosed Scope:
    When a function is nested within another function, variables declared in the outer function can be accessed within the inner function. These variables are in the enclosing scope. In our example, enclosed_fn defines the variable enclosed_var, which is in the enclosing scope for local_fn.

  3. Local Scope:
    Variables declared within a function are considered local variables with a lifecycle limited to the execution of that function. In our example, local_fn defines the variable local_var, which has local scope.

  4. Built-in Scope:
    The scope that includes pre-defined attribute, such as the print(), str(), len(), range() function. These are available in any part of your Python code, without the need for any explicit import or declaration.

Takeaways from the Code:

  • The enclosed_fn can access variables in both global and enclosed scopes.
  • The local_fn (nested within enclosed_fn) can access variables in global, enclosed, and local scopes.
  • The built-in global function print() can access global_var from the global scope.

Conclusion:

Understanding variable scope in Python is essential for managing the interactions among different parts of your code. By examining this example with nested functions, we have demonstrated how variables in global, enclosed, and local scopes can be accessed by functions on different levels. This knowledge will enable you to write cleaner and more efficient Python programs.

Top comments (0)