What is Variable Scope?

In programming, a variable is like a box where you can store values (numbers, text, etc.). Variable scope refers to where a variable can be accessed or used in your code. It’s all about the “reach” of the variable—where in your code you can see or change the value of that variable.

In Python, there are mainly four types of variable scope:

  1. Local Scope
  2. Enclosing Scope
  3. Global Scope
  4. Built-in Scope

Let’s dive into each one.


1. Local Scope

When you create a variable inside a function, that variable is only accessible within that function. We say the variable has a local scope. It’s like you put the variable in a small box, and the box can only be used inside the function.

Example:

def greet():
    message = "Hello, world!"  # Local variable
    print(message)

greet()  # This will work and print the message

Output:

Hello, world!

In this example, message is a local variable inside the function greet(). It’s created when the function runs, and it only exists while the function is running. Outside the function, you can’t use it.


2. Enclosing Scope

Sometimes, you might have a function inside another function. The inner function can access variables from the outer function (but not vice versa). This is called enclosing scope. It’s like a box inside another box. The inner box can see the contents of the outer box.

Example:

def outer_function():
    outer_message = "I am the outer function"  # Variable in outer function

    def inner_function():
        print(outer_message)  # Inner function can access outer function's variable

    inner_function()  # Calling the inner function

outer_function()

Output:

I am the outer function

In this example, the inner function inner_function() can see and use the outer_message variable, which is defined in the outer function outer_function(). But the outer function can’t access variables inside the inner function unless passed.


3. Global Scope

A global variable is a variable that is defined outside of any function. It is accessible from anywhere in your code—inside functions and outside of them. Think of it as a box that you place outside of all other boxes, and everyone can see and use it.

Example:

global_message = "I am a global variable"  # Global variable

def greet():
    print(global_message)  # Can access global variable

greet()  # This will print the global variable
print(global_message)  # You can also use the global variable outside the function

Output:

I am a global variable
I am a global variable

In this example, global_message is a global variable because it’s defined outside any function. Both the function greet() and the main part of the code can use it.


4. Built-in Scope

Python also has built-in variables and functions that are available for you to use anywhere in your code. These are the functions and variables that Python gives you, like print() or len(). They’re part of Python’s built-in scope.

Example:

print("Hello, Python!")  # print() is a built-in function

Output:

Hello, Python!

Here, print() is a built-in function, which means Python has already defined it for you, and you can use it at any time in your code.


How Python Looks for Variables: The LEGB Rule

Python looks for variables in a specific order when you reference them. It follows the LEGB rule:

  1. Local scope (inside the current function)
  2. Enclosing scope (any enclosing functions)
  3. Global scope (outside all functions, in the main body)
  4. Built-in scope (Python’s pre-defined functions and variables)

Let’s look at an example to understand this.

Example:

x = 10  # Global variable

def outer_function():
    x = 20  # Enclosing variable
    
    def inner_function():
        x = 30  # Local variable
        print(x)  # Which x will this print?

    inner_function()

outer_function()

Output:

30

Here’s the breakdown:

  1. Python first checks if x is in the local scope inside inner_function(). It finds it there, so it prints 30.
  2. If x wasn’t found there, Python would check the enclosing scope, which would be outer_function(), but it’s not needed because it already found x inside inner_function().

Modifying Global Variables Inside a Function

If you want to modify a global variable inside a function, you have to use the global keyword to tell Python that you’re using the global variable, not creating a new local one.

Example:

x = 5  # Global variable

def change_global():
    global x  # Using the global variable
    x = 10  # Modify global variable

change_global()
print(x)  # Now x is modified globally

Output:

10

In this example, x is initially 5. When we use the global keyword, we modify the global x inside the function. After calling the function, x becomes 10 in the global scope.