5.4 Local and global scope of a variable (BT101CO)
In Python, the Scope of a variable refers to the specific region of a program where that variable is recognized and can be accessed. A variable's scope is determined by where it is first defined.
1. Local vs. Global Scope
Local Scope
A variable defined inside a function has a local scope. It is created when the function starts and is destroyed as soon as the function finishes execution.
- Access: It can only be accessed inside that specific function.
- Isolation: You can have a local variable with the same name in different functions without interference.
Global Scope
A variable defined outside of all functions (usually at the top of the script) has a global scope.
- Access: It can be accessed by any function within the same program.
- Lifetime: It exists as long as the entire program is running.
2. The global Keyword
By default, you can read a global variable inside a function, but you cannot modify it. If you try to assign a new value to a global variable name inside a function, Python simply creates a new local variable with that same name.
To modify a global variable inside a function, you must use the global keyword.
count = 0 # Global
def increment():
global count # Tells Python to use the global 'count'
count += 1
increment()
print(count) # Output: 1
3. Shadowing (Variable Masking)
If a local variable and a global variable have the same name, the local variable "shadows" or masks the global one within that function's scope.
name = "Global Ashok"
def greet():
name = "Local Amit" # Shadowing occurs here
print(name)
greet() # Output: Local Amit
print(name) # Output: Global Ashok
4. The LEGB Rule (Search Hierarchy)
The LEGB Rule is the specific hierarchy Python uses to search for a variable name. It searches through four distinct layers of scope in a very specific order:
- Local (L): Names assigned within a function.
- Enclosing (E): Names in the local scope of any enclosing (outer) functions.
- Global (G): Names assigned at the top-level of a module file.
- Built-in (B): Names pre-installed in the Python interpreter (e.g.,
print,len).
Example of LEGB in action:
x = "Global Value"
def outer_func():
x = "Enclosing Value"
def inner_func():
x = "Local Value"
print(x) # Python finds 'x' in Local (L) and stops.
inner_func()
outer_func() # Output: Local Value
5. The nonlocal Keyword
Just as we use global to modify a global variable, we use nonlocal to modify a variable in the Enclosing scope of a nested function.
def counter():
count = 0 # Enclosing scope
def increment():
nonlocal count
count += 1
return count
return increment
my_counter = counter()
print(my_counter()) # Output: 1
Summary Table: Scope Types
| Scope | Defined In | Accessibility | Search Order |
|---|---|---|---|
| Local (L) | Function body | Within that function only | 1st |
| Enclosing (E) | Outer function | Within inner functions | 2nd |
| Global (G) | Module level | Throughout the program | 3rd |
| Built-in (B) | Python itself | Available everywhere | 4th |