Scope

Scope refers to the areas where variables are visible and accessible. Variables that can be accessed anywhere in a Python program are in the global scope. Conversely, variables that are defined within the body of structures like classes and methods exist only in the local scope.
Local Scope
Suppose a variable is initialized within a function. This variable can only be used within that function and not from outside the function.
def my_function():x = 200print(x)my_function()
Nested Functions and Local Scope
In the example below, a variable x
is defined within the local scope of the outer_function()
function, followed by a defined inner_function()
function. Since inner_function()
exists within the local scope of outer_function()
, x
can be accessed and printed within inner_function()
:
def outer_function():x = 200# Initialized in outer functiondef inner_function():print(x)inner_function()outer_function()# Output: 200
Global Scope
A variable initialized in the main body is defined as a global variable and can be used anywhere in the code, including nested blocks, loops, etc. This is because these variables exist in the global scope of the code.
All contributors
- BrandonDusch580 total contributions
- vivek_prajapatii4 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.