In Python, scope
refers to the region of a program where a particular variable or function is accessible
.
Understanding scope is crucial because it determines the visibility and lifetime of variables.
There are four main types of scopes in Python:
- Local Scope
- Enclosing Scope
- Global Scope
- Built-in Scope
Local Scope
Variables defined inside a function are in the local scope. They are only accessible within that function.
def my_function(): local_var = "I am local" print(local_var) my_function() # Output: I am local # print(local_var) # Uncommenting this will raise NameError: name 'local_var' is not defined
Enclosing Scope
This refers to the scope of enclosing functions, also known as nonlocal scope. It is relevant in nested functions.
def outer_function(): outer_var = "I am outer" def inner_function(): print(outer_var) # Accessing variable from enclosing (outer) function inner_function() outer_function() # Output: I am outer
Global Scope
Variables defined at the top level of a module or script, outside any function, are in the global scope. They are accessible from any function within the module.
global_var = "I am global" def my_function(): print(global_var) my_function() # Output: I am global print(global_var) # Output: I am global
Built-in Scope
This is a special scope that contains built-in functions and exceptions provided by Python.
print(len([1, 2, 3])) # len is a built-in function
Example Illustrating All Scopes
global_var = "I am global" def outer_function(): outer_var = "I am outer" def inner_function(): inner_var = "I am inner" print(global_var) # Accessing global scope print(outer_var) # Accessing enclosing (outer) scope print(inner_var) # Accessing local scope inner_function() # print(inner_var) # Uncommenting this will raise NameError: name 'inner_var' is not defined outer_function()
global
and nonlocal
Keywords
The global
keyword is used to modify a global variable inside a function. The nonlocal
keyword is used to modify a variable in the enclosing scope.
Using global
Keyword
global_var = "I am global" def my_function(): global global_var global_var = "I have been modified globally" print(global_var) my_function() # Output: I have been modified globally print(global_var) # Output: I have been modified globally
Using nonlocal
Keyword
def outer_function(): outer_var = "I am outer" def inner_function(): nonlocal outer_var outer_var = "I have been modified in enclosing scope" print(outer_var) inner_function() print(outer_var) outer_function() # Output: I have been modified in enclosing scope # Output: I have been modified in enclosing scope
Summary
- Local Scope: Variables defined inside a function. Accessible only within that function.
- Enclosing Scope: Variables in the local scope of enclosing functions. Relevant in nested functions.
- Global Scope: Variables defined at the top level of a module or script. Accessible from any function in the module.
- Built-in Scope: Special scope containing built-in functions and exceptions provided by Python.
global
keyword: Allows modification of global variables inside a function.nonlocal
keyword: Allows modification of variables in the enclosing scope inside nested functions.
scope is essential for managing the visibility and lifetime of variables in Python programs, ensuring proper variable usage and avoiding unintended side effects.