0% completed
Variable scope refers to the region of a program where a variable is accessible, while a namespace is a container for a set of identifiers (such as variable and function names) and manages ensuring that all names within it are unique. Python organizes these names into namespaces, and the scope of variables is determined by the location where the variable is defined.
Python primarily has three types of scopes, which define the accessibility of variables. These scopes are:
These scopes are understood through the LEGB rule (Local, Enclosing, Global, Built-in), which Python uses to determine which namespace to check first for a variable name.
Local scope refers to variables that are defined within a function. These variables are only accessible within the function they are declared in and are destroyed once the function completes execution.
In this example, we will demonstrate the use of local scope within a function.
Explanation:
local_var = "I am local"
: A local variable local_var
is defined inside the function local_scope_example
.print(local_var)
: Inside the function, local_var
is accessible and printed.local_scope_example()
executes the function, showing the output of the print statement.local_var
outside its local scope results in an error (as noted in the commented-out line).Global scope refers to variables defined at the top level of a module or script, outside of any functions. These variables are accessible from any part of the code, including functions.
In this example, we will introduce the global scope by defining and accessing a global variable.
Explanation:
global_var = "I am global"
: A global variable global_var
is defined at the top level of the module.access_global()
, global_var
is accessible and is printed, demonstrating that functions can access global variables.global_var
is again printed outside any function, confirming its global accessibility.The global
keyword is used to modify global variables from within a function. It is necessary when you want to change the value of a global variable inside a function.
In this example, we will demonstrate modifying a global variable using the global
keyword.
Explanation:
x = "original global"
: The global variable x
is initially set to "original global".global x
: Inside the function modify_global()
, the global
keyword specifies that x
refers to the global variable.x = "modified global"
: The global variable x
is modified within the function.print
statements before and after the function call demonstrate the change in the value of the global variable x
due to the function execution.Enclosing scope comes into play in scenarios involving nested functions, where an inner function has access to the variables defined in the outer function. This allows the inner function to access and modify variables from the outer function.
In this example, we will demonstrate the use of enclosing scope in nested functions.
Explanation:
outer_var = "I am from the outer function"
: A variable is defined in the outer function.inner_function
accesses outer_var
from its enclosing (outer) function, demonstrating how variables in the outer scope can be accessed by the nested inner function.outer_var
, confirming its scope is limited to the outer function and its inner functions.The nonlocal
keyword is used to work with variables in nested functions where you need to modify a variable defined in an outer (but non-global) scope.
In this example, we will use the nonlocal
keyword to modify a variable in an enclosing scope.
Explanation:
outer_var = "original outer var"
: Defines a variable in the outer function.nonlocal outer_var
: The nonlocal
statement in the inner function indicates that outer_var
refers to the same variable defined in the nearest enclosing scope.outer_var = "modified by inner"
: The inner function modifies outer_var
, and the change is reflected when accessed in the outer function.Understanding these advanced aspects of Python’s scoping rules enhances your capability to manage data effectively across different parts of a program, especially in complex applications with nested functions and variable interactions. This knowledge is pivotal for writing clean, efficient, and bug-free code.
.....
.....
.....