last modified February 26, 2025
Variable shadowing occurs when a variable declared within a certain scope (e.g., a function or block) has the same name as a variable declared in an outer scope. This can lead to confusion and bugs, as the inner variable "shadows" the outer one, making the outer variable inaccessible within the inner scope. This tutorial covers variable shadowing in Python, including examples and best practices to avoid it.
Variable shadowing happens when a variable in a local scope (e.g., inside a function) has the same name as a variable in an outer scope (e.g., global or enclosing scope). The local variable "shadows" the outer variable, making it inaccessible within the local scope. This can lead to unintended behavior and bugs in your code.
This example demonstrates variable shadowing within a local scope (inside a function).
x = 10 # Global variable
def my_function():
x = 20 # Local variable shadows the global variable
print(f"Local x: {x}")
my_function()
print(f"Global x: {x}")
# Output:
# Local x: 20
# Global x: 10
In this example, the local variable x inside
my_function shadows the global variable x. The global
variable remains unchanged outside the function.
This example demonstrates variable shadowing in nested functions.
x = 10 # Outer scope variable
def outer_function():
x = 20 # Enclosing scope variable
def inner_function():
x = 30 # Local variable shadows the enclosing scope variable
print(f"Inner x: {x}")
inner_function()
print(f"Outer x: {x}")
outer_function()
print(f"Global x: {x}")
# Output:
# Inner x: 30
# Outer x: 20
# Global x: 10
In this example, the local variable x in
inner_function shadows the enclosing scope variable x
in outer_function. Each scope maintains its own value of
x.
This example demonstrates variable shadowing within a loop.
x = 10 # Global variable
for x in range(5): # Loop variable shadows the global variable
print(f"Loop x: {x}")
print(f"Global x: {x}")
# Output:
# Loop x: 0
# Loop x: 1
# Loop x: 2
# Loop x: 3
# Loop x: 4
# Global x: 4
In this example, the loop variable x shadows the global variable
x. After the loop, the global variable retains the last value
assigned to the loop variable.
This example demonstrates variable shadowing in class attributes.
x = 10 # Global variable
class MyClass:
x = 20 # Class attribute shadows the global variable
def print_x(self):
print(f"Class x: {self.x}")
print(f"Global x: {x}")
obj = MyClass()
obj.print_x()
# Output:
# Class x: 20
# Global x: 10
In this example, the class attribute x shadows the global variable
x. The global variable is still accessible using its name directly.
This example demonstrates variable shadowing in list comprehensions.
x = 10 # Global variable
# List comprehension variable shadows the global variable
squares = [x ** 2 for x in range(5)]
print(f"Squares: {squares}")
print(f"Global x: {x}")
# Output:
# Squares: [0, 1, 4, 9, 16]
# Global x: 10
In this example, the variable x in the list comprehension shadows
the global variable x. The global variable remains unchanged after
the list comprehension.
list, str) as variable names.Python Scopes and Namespaces Documentation
In this article, we have explored variable shadowing in Python and demonstrated its occurrence in different scopes through practical examples.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.