Python From Beginner to Advanced

0% completed

Previous
Next
Python - Variable scope

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.

Types of Scopes in Python

Python primarily has three types of scopes, which define the accessibility of variables. These scopes are:

  1. Local Scope
  2. Enclosing Scope
  3. Global Scope

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

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.

  • Variables are created when a function starts and destroyed when it ends.
  • Local variables cannot be accessed outside the function in which they are declared.

Example

In this example, we will demonstrate the use of local scope within a function.

Python3
Python3

. . . .

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.
  • Calling local_scope_example() executes the function, showing the output of the print statement.
  • Attempting to access local_var outside its local scope results in an error (as noted in the commented-out line).

Global Scope

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.

  • Variables are available throughout the execution of the program.
  • Global variables can be accessed and modified from any part of the program, including inside functions.

Example

In this example, we will introduce the global scope by defining and accessing a global variable.

Python3
Python3

. . . .

Explanation:

  • global_var = "I am global": A global variable global_var is defined at the top level of the module.
  • Inside access_global(), global_var is accessible and is printed, demonstrating that functions can access global variables.
  • After the function call, global_var is again printed outside any function, confirming its global accessibility.

The global Keyword

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.

  • Allows modifications to global variables inside functions.
  • Essential when local scope needs to interact with global variables.

Example

In this example, we will demonstrate modifying a global variable using the global keyword.

Python3
Python3

. . . .

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.
  • The 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

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.

  • Relevant in nested functions where the inner function can access variables of the outer function.
  • Helps maintain a variable's state between calls to the inner function without using global variables.

Example

In this example, we will demonstrate the use of enclosing scope in nested functions.

Python3
Python3

. . . .

Explanation:

  • outer_var = "I am from the outer function": A variable is defined in the outer function.
  • The 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.
  • Both the inner function and the outer function print the value of outer_var, confirming its scope is limited to the outer function and its inner functions.

The nonlocal Keyword

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.

  • Used to declare that a variable is not local to the nested (inner) function, nor is it global, but belongs to an enclosing scope.
  • Crucial for modifying variables in a middle layer in nested function scenarios.

Example

In this example, we will use the nonlocal keyword to modify a variable in an enclosing scope.

Python3
Python3

. . . .

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.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next