Scope and namespace
=> Scope and namespace are related concepts in Python, but they are not the same.
Scope
- defines the visibility of a name within a block of code.
- determines where you can access a variable or function name.
Python has four types of scopes:
- Local scope: Variables defined within a function.
- Enclosing scope: Variables defined in the enclosing function (for nested functions).
- Global scope: Variables defined at the top level of a module or explicitly declared as global within a function.
- Built-in scope: Names predefined in the Python built-in module.
Namespace
- refers to a mapping from names to objects.
- It’s like a dictionary where the keys are the names of variables and the values are the objects those names refer to. Namespaces are used to avoid naming conflicts. Python has various namespaces, such as
- global namespace (module-level),
- local namespace (function-level),
- class namespace, etc.
concept of scope and how it relates to functions.
scope refers to the region of a program where a particular identifier (such as a variable name) is recognized and can be accessed. The concept of scope is crucial for understanding how variables are accessed and modified within functions.
Python follows the LEGB rule to determine the scope of a variable:
Local scope (L):
This is the innermost scope, which includes names defined within the current function.
Variables created inside a function are only accessible within that function's scope.
Enclosing scope (E):
This refers to the scope of the enclosing function(s), for nested functions.
If a function is defined inside another function (nested function),
it can access variables from the enclosing scope.
Global scope (G):
This scope includes names defined at the top level of the module or
declared as global within a function.
Variables defined outside of any function or
explicitly declared as global within a function are in the global scope.
Global variables can be accessed from anywhere in the module.
Built-in scope (B):
This is the outermost scope, which includes names predefined in built-in modules.
These names are available globally to all Python code.
When a variable is referenced within a function, Python searches for it in the following order: local scope, enclosing scope, global scope, and built-in scope. If the variable is found in any of these scopes, its value is used. If the variable is not found in any of the scopes, Python raises a NameError.
local and global variables and their visibility within functions.
x = 10 # Global variable
def outer_function():
y = 20 # Enclosing scope variable
def inner_function():
z = 30 # Local variable
print("Inside inner_function:", x, y, z)
# Accessing variables from different scopes
inner_function()
print("Inside outer_function:", x, y)
# Accessing variables from different scopes
outer_function()
print("Global scope:", x)
# Accessing a global variable
'''
x is a global variable accessible from anywhere in the module.
y is an enclosing scope variable, accessible within the outer_function() but not outside of it.
z is a local variable, accessible only within the inner_function().
'''
variable lifetime and how it affects the availability of variables in different scopes.
need to come up with a good example