Types of Variable Scope and Lifetime in Python Example

In Python, the scope of a variable refers to the part of the program where the variable is accessible or can be used. The lifetime of a variable refers to the duration for which the variable exists in memory.

types of variable scope in python

In Python, there are two types of variable scope: global scope and local scope.

what is local scope in python :- In Python, a local scope is a part of the program where a local variable is defined and can be used. A local variable is a variable that is defined inside a function or class, and it is only accessible within that function or class.

what is global scope in python :- In Python, a global scope is the part of the program where a global variable is defined and can be accessed from anywhere in the program. A global variable is a variable that is defined outside of any function or class, and it is accessible from anywhere in the program.

Here is an example of how to demonstrate the scope and lifetime of variables in Python:

x = 10  # global variable

def func():
    y = 20  # local variable
    
    # Print the value of the global and local variables
    print("Global variable x:", x)
    print("Local variable y:", y)

func()

# Try to access the local variable outside the function
print("Local variable y:", y)  # this will give an error

The output of the above code would be:

Global variable x: 10
Local variable y: 20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined

In the above example, the global variable x is defined outside the function and it is accessible inside the function. The local variable y is defined inside the function and it is only accessible within the function. When the function is called, the value of x and y are printed. When you try to access the local variable y outside the function, you get an error because y is not defined in the global scope.

The lifetime of a variable in Python is determined by its scope. In the above example, the global variable x has a lifetime that is the same as the lifetime of the program, while the local variable y has a lifetime that is only within the function. Once the function returns, the local variable y is no longer in memory and it is no longer accessible.

enclosed scope in python

In Python, an enclosed scope is a part of the program where a variable is defined and can be accessed from within that block and any inner blocks within it. An inner block is a block of code that is defined inside another block of code.

Here is an example of how to define and use variables in an enclosed scope in Python:

x = 10  # global variable

def func1():
    y = 20  # local variable
    
    def func2():
        # Declare a variable in the enclosed scope
        z = 30
        
        # Print the values of the variables in the enclosed scope
        print("Enclosed variable z:", z)
        print("Local variable y:", y)
        print("Global variable x:", x)
    
    # Call the inner function
    func2()

# Call the outer function
func1()

The output of the above code would be:

Enclosed variable z: 30
Local variable y: 20
Global variable x: 10

In the above example, the global variable x is defined outside the functions and it is accessible from anywhere in the program. The local variable y is defined inside func1 and it is only accessible within that function. The variable z is defined inside func2, which is an inner function defined inside func1. The variable z is in the enclosed scope of func2, which means it is accessible from within func2 and any inner blocks within it. When func2 is called, the values of z, y, and x are printed.

Module level scope in Python

In Python, a module is a file that contains Python code, and the module level scope is the part of the module where variables are defined and can be accessed from anywhere within the module.

What is a module variable vs. a global variable in Python

In Python, a module is a file that contains Python code, and a module variable is a variable that is defined at the module level, which means it is defined outside of any function or class within the module. A global variable, on the other hand, is a variable that is defined outside of any function or class and is accessible from anywhere in the program.

The scope of a global variable is global to the entire program, while the scope of a module variable is limited to the module in which it is defined. In other words, a module variable is accessible from anywhere within the module, but it is not necessarily accessible from other modules or the global scope.

Here is an example to illustrate the difference between a global variable and a module variable:

# Declare a global variable
x = 10

def func():
    # Print the value of the global variable
    print(x)

# Print the value of the global variable
print(x)

# Call the function
func()

# Define a module level variable in another module
other_module.y = 20

# Try to access the module level variable from the global scope
print(y)  # this will give an error

In the above example, the global variable x is defined outside the function and it is accessible from anywhere in the program. When the global variable x is printed outside the function, its value is 10. When the function is called, the value of x is also printed.

The module other_module contains a module level variable y, which is defined at the module level and is only accessible within that module. When you try to access the module level variable y from the global scope, you get an error because y is not defined in the global scope or in the current module. The scope of a module level variable is limited to the module in which it is defined, and it is not accessible from other modules or the global scope.

To access a module level variable from another module, you need to import the module and use the dot notation to access the variable. For example, to access the module level variable y from the other_module, you can do the following:

# Import the other module
import other_module

# Access the module level variable using the dot notation
print(other_module.y)

Leave a Comment