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:
- Global Scope
- 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:
1 2 3 4 5 6 7 8 9 10 | x = 10 # x is a global variable def foo(): y = 20 # y is a local variable print(x) # x can be accessed from within the function print(y) # y can be accessed from within the function foo() print(x) # x can be accessed from outside the function print(y) # y is not defined in the global scope, so this will raise an error |
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:
1 2 3 4 5 6 7 8 9 10 | x = 10 # x is a global variable def foo(): global x # x is now a global variable x = 20 # this will modify the global variable x y = 30 # y is a local variable foo() print(x) # prints 20 print(y) # y is not defined in the global scope, so this will raise an error |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | def foo(): x = 10 # x is a local variable in the outer function def bar(): nonlocal x # x is now a nonlocal variable in the inner function x = 20 # this will modify the nonlocal variable x y = 30 # y is a local variable in the inner function bar() print(x) # prints 20 print(y) # y is not defined in the outer function, so this will raise an error foo() |
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
orstr
). If it is a built-in variable, Python will use its value.
Here is an example to illustrate the LEGB rule:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | x = 10 # x is a global variable def foo(): y = 20 # y is a local variable in the outer function def bar(): z = 30 # z is a local variable in the inner function print(x) # prints 10 (global scope) print(y) # prints 20 (enclosing scope) print(z) # prints 30 (local scope) print(len) # prints the built-in function len bar() foo() |
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.