Python Variable Scope Resolution

Posted by

Python Variable Scope Resolution is part of the program where it is defined and can be accessed. There are two types of scope in Python:

  1. Global Scope
  2. Local Scope

Global Scope

A variable that is defined outside of any function or class is said to be defined in the global scope and is known as a global variable. Global variables can be accessed from anywhere in the code, including inside functions and classes.

Local Scope

A variable that is defined inside a function or class is said to be defined in the local scope and is known as a local variable. Local variables can only be accessed from within the function or class in which they are defined.

When you try to access a variable, Python will first look for it in the local scope. If it is not found there, it will look for it in the global scope. If it is not found there either, it will raise an error. This is known as the “scope resolution” process.

Here is an example to illustrate the concept of variable scope resolution in Python:

In this example, x is a global variable that can be accessed both from within the function foo and from outside it. y is a local variable that can only be accessed from within the function foo . When you try to access y from outside the function, Python will raise an error because y is not defined in the global scope.

The global keyword

You can use the global keyword to define a variable as a global variable inside a function or class.

For example:

In this example, x is defined as a global variable inside the function foo. This means that any changes made to x inside the function will be reflected in the global scope.

The nonlocal keyword:

In Python, you can also define variables in the enclosing scope of a nested function using the nonlocal keyword.

For example:

In this example, x is defined as a nonlocal variable in the inner function bar . This means that any changes made to x inside the inner function will be reflected in the enclosing scope (in this case, the outer function foo ).

The LEGB rule

In Python, the scope of a variable is determined using the “LEGB” rule, which stands for “Local, Enclosing, Global, Built-in”. This rule determines the order in which Python searches for variables when you try to access them.

Here is a brief explanation of each part of the rule:

  • Local: When you try to access a variable, Python will first look for it in the local scope of the current function or class. If it is found there, Python will use the value of the local variable.
  • Enclosing: If the variable is not found in the local scope, Python will look for it in the enclosing scope of any nested functions. This means that Python will look for the variable in the local scope of the outer function, then in the enclosing scope of that function, and so on until it reaches the global scope.
  • Global: If the variable is not found in the local or enclosing scopes, Python will look for it in the global scope. If it is found there, Python will use the value of the global variable.
  • Built-in: If the variable is not found in any of the local, enclosing, or global scopes, Python will check if it is a built-in variable (such as len or str ). If it is a built-in variable, Python will use its value.

Here is an example to illustrate the LEGB rule:

In this example, x is a global variable, y is a local variable in the outer function foo , and z is a local variable in the inner function bar . When you try to access x , y , and z from within the inner function bar , Python will use the value of the global variable x , the local variable y in the enclosing scope of the inner function, and the local variable z in the local scope of the inner function, respectively.

Leave a Reply

Your email address will not be published. Required fields are marked *