- Understanding Scope in Python
- The LEGB Rule (Local, Enclosing, Global, Built-in)
- Global and Nonlocal Keywords
- Namespace and Variable Resolution
- Summary
- Tasks
Scope refers to the region of the code where a variable is accessible. In Python, there are different types of scopes: local, enclosing, global, and built-in. Understanding scope is crucial for writing clean and bug-free code.
Variables defined inside a function are in the local scope and can only be accessed within that function.
def my_function():
x = 10 # Local scope
print(x)
my_function()
print(x) # This will raise an error because x is not defined outside the function
Output:
10
NameError: name 'x' is not defined
Explanation: The variable x
is defined inside the function my_function
and is not accessible outside the function.
The LEGB rule defines the order in which Python looks for variables. It stands for Local, Enclosing, Global, and Built-in.
Variables defined inside a function.
def my_function():
x = 10 # Local scope
print(x)
my_function()
Output:
10
Variables in the local scope of enclosing functions.
def outer_function():
x = 20 # Enclosing scope
def inner_function():
print(x)
inner_function()
outer_function()
Output:
20
Variables defined at the top level of a script or module.
x = 30 # Global scope
def my_function():
print(x)
my_function()
print(x)
Output:
30
30
Names that are preassigned in Python.
print(len("Hello")) # Built-in function
Output:
5
The global
and nonlocal
keywords are used to modify the behavior of variables in different scopes.
The global
keyword allows you to modify a global variable inside a function.
x = 40
def my_function():
global x
x = 50
my_function()
print(x)
Output:
50
Explanation: The global
keyword allows the function to modify the global variable x
.
The nonlocal
keyword allows you to modify a variable in the enclosing scope.
def outer_function():
x = 60
def inner_function():
nonlocal x
x = 70
inner_function()
print(x)
outer_function()
Output:
70
Explanation: The nonlocal
keyword allows the inner function to modify the variable x
in the enclosing scope.
A namespace is a container that holds a set of identifiers (variable names) and their corresponding objects. Python uses namespaces to keep track of variables and their values.
x = 80 # Global namespace
def my_function():
x = 90 # Local namespace
print(x)
my_function()
print(x)
Output:
90
80
Explanation: The variable x
in the global namespace is different from the variable x
in the local namespace of the function.
Python resolves variables using the LEGB rule.
x = 100 # Global scope
def outer_function():
x = 110 # Enclosing scope
def inner_function():
x = 120 # Local scope
print(x)
inner_function()
outer_function()
Output:
120
Explanation: The variable x
in the local scope of inner_function
is used, as it is the closest scope according to the LEGB rule.
In this chapter, we covered scope and namespace in Python, including the LEGB rule, the global
and nonlocal
keywords, and how Python resolves variables.
- Write a program that demonstrates the use of local, enclosing, and global scopes.
- Write a program that uses the
global
keyword to modify a global variable inside a function. - Write a program that uses the
nonlocal
keyword to modify a variable in the enclosing scope. - Write a program that demonstrates variable resolution using the LEGB rule.