Python’s scope rules dictate where and how you can access variables within your code. Mastering scope is important for writing clean, bug-free, and maintainable Python programs. Let’s look at the different levels of scope in Python and illustrate them with practical examples.
Levels of Scope in Python
Python uses the LEGB rule to determine the scope of a variable:
Local: This is the innermost scope, defined within a function or block of code (like a loop or conditional statement). Variables defined here are only accessible within that specific function or block.
Enclosing function locals: If a variable isn’t found locally, Python searches the enclosing function’s scope. This applies to nested functions—inner functions can access variables from their outer functions.
Global: This scope encompasses variables defined at the top level of a module (a
.py
file). These variables are accessible from anywhere within that module, but not from other modules unless explicitly imported.Built-in: This is the outermost scope, containing pre-defined functions and constants available in Python (e.g.,
print
,len
,True
).
Code Examples Illustrating Scope
Let’s illustrate these scope levels with code:
Example 1: Local Scope
def my_function():
= 10 # Local variable
x print(x)
# Output: 10
my_function() print(x) # This will raise a NameError because x is not defined in the global scope
In this example, x
is only accessible within my_function()
.
Example 2: Enclosing Function Locals (Nested Functions)
def outer_function():
= 20 # Enclosing function variable
y
def inner_function():
print(y) # Accessing y from the enclosing function
inner_function()
# Output: 20 outer_function()
inner_function()
can access y
because it’s in its enclosing function’s scope.
Example 3: Global Scope
= 30 # Global variable
z
def my_function():
print(z) # Accessing the global variable
# Output: 30 my_function()
my_function()
can directly access the global variable z
.
Example 4: Modifying Global Variables from Within a Function
To modify a global variable inside a function, you need to use the global
keyword:
= 40
global_var
def modify_global():
global global_var # Declare global_var as a global variable
= 50
global_var
modify_global()print(global_var) # Output: 50
Example 5: The nonlocal
Keyword
The nonlocal
keyword is used to modify variables in enclosing functions within nested functions.
def outer():
= 10
a def inner():
nonlocal a
= 20
a
inner()print(a) # Output: 20
outer()
Without nonlocal
, assigning to a
within inner()
would create a new local variable, leaving the a
in outer()
unchanged.
Understanding Scope for Better Code
By understanding Python’s scope rules, you can write more organized, predictable, and maintainable code. Proper scope management helps avoid naming conflicts and makes your code easier to debug and understand. Careful consideration of scope is particularly important when working with larger, more complex projects.