Python Variable Scope – Local, Global, Built-in, Enclosed
Learn Python Variable Scope: Local, Global, Enclosing & Built-in. Understand global & nonlocal keywords. Let’s dive in!
🔍 What is Python Variable Scope?
The scope of a variable refers to the region of the code where that variable is recognized or accessible. Within this scope, you can use the variable without any special prefixes.
Example:
>>> b = 8 >>> def func(): a = 7 print(a) print(b) >>> func()
Output:
7 8
>>> a
Output:
Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> a NameError: name 'a' is not defined
Also, the duration during which a variable exists is called its lifetime.
🔸 Types of Variable Scope in Python
There are 4 main types of variable scope in Python:
Local Scope
Global Scope
Enclosing Scope
Built-in Scope
Let’s explore each of them one by one:
1️⃣ Local Scope in Python
In the previous example, variable a was defined inside the function func. So, a is local to func. It is only accessible within the function, not outside.
Example:
>>> a = 0 >>> def func(): print(a) a = 1 print(a) >>> func()
Output:
Traceback (most recent call last): File "<pyshell#79>", line 1, in <module> func() File "<pyshell#78>", line 2, in func print(a) UnboundLocalError: local variable 'a' referenced before assignment
Although a was initially defined globally, the function also tries to define a local variable a, which causes a conflict. This results in an UnboundLocalError.
It's best practice to avoid modifying global variables within functions. Instead, pass them as arguments.
Better approach:
>>> def func(a=0): a += 1 print(a) >>> func()
Output:
1
2️⃣ Global Scope in Python
A variable defined outside any function or block is considered to be in the global scope.
Example:
>>> b = 5 >>> def show(): print(b) >>> show()
You can access global variables inside functions, but if you want to modify them, you must declare them as global using the global keyword.
3️⃣ Enclosing Scope in Python
This is also called the nonlocal scope. It refers to the scope of a function enclosing another function.
Example:
>>> def red(): a = 1 def blue(): b = 2 print(a) print(b) blue() print(a) >>> red()
Output:
1 2 1
Here, b is local to blue(), while a is nonlocal to blue() but local to red().
4️⃣ Built-in Scope
This is the widest scope, where all built-in names in Python (like print(), id(), etc.) reside. These are available as soon as the Python interpreter is launched—no need to import any module.
🧠 Global Keyword in Python
To modify a global variable inside a function, use the global keyword.
Without global:
>>> a = 1 >>> def counter(): a = 2 print(a) >>> counter()
Output:
2
>>> a
Output:
1
Here, a = 2 inside the function created a new local variable, leaving the global a unchanged.
With global:
>>> a = 1 >>> def counter(): global a a = 2 print(a) >>> counter()
Output:
2
>>> a
Output:
2
Using global, we tell Python to use the global version of a instead of creating a new local variable.
🧠 Nonlocal Keyword in Python
Similar to global, if you want to modify a variable in the enclosing (nonlocal) scope, use the nonlocal keyword.
Without nonlocal:
>>> def red(): a = 1 def blue(): a = 2 b = 2 print(a) print(b) blue() print(a) >>> red()
Output:
2 2 1
With nonlocal:
>>> def red(): a = 1 def blue(): nonlocal a a = 2 b = 2 print(a) print(b) blue() print(a) >>> red()
Output:
2 2 2
✅ Python Interview Questions on Variable Scope
What do you mean by scope?
What are the types of variable scope?
What is the scope of a variable with an example?
What are Python variables?
How do you declare a variable in Python 3?
🎯 Conclusion
We hope this guide helped you clearly understand the scope of variables in Python. We covered:
Local Scope
Global Scope
Enclosing (Nonlocal) Scope
Built-in Scope
Write A Comment
No Comments