The article covers the concept of variable scope in Python.
In Python, the scope of a variable refers to the part of the program where the variable is visible or can be accessed. There are two types of variable scope in Python.
The first is the local scope, which exists within a local body (like a function), and the second is the global scope which exists throughout the Python program.
Local Scope
A local scope is created when a variable is declared within a function. What does this mean? What effect does this have? First look at the example below.
def inc(x):
result = x + 1
inc(5)
print(result)
The above will produce the following error.
Traceback (most recent call last):
File "C:\Users\Default\Desktop\VariableScope.py", line 8, in <module>
print(x)
NameError: name 'result' is not defined
The error states that x
is not defined, or in other words, x
is not a recognized variable. The truth is, that variables declared in a function are not visible to anything outside of it. This area within the function is what we call the local scope.
This allows for two variables (or more) of the same name to be created, snice they exist in different scopes. Variable x
may exist in a function, and Variable x
may also exist in the main python body under the same name.
To maintain readability and avoid confusion however, keep your variables name unique and meaningful, even if they are in different variable scopes.
Global Scope
A variable created in the main body of the Python program global variable belongs to the global scope and is accessible from any part of the code. In other words, global variables are accessible from within any scope, global or local.
See the below example, where the function is able to access variables from the main Python code.
def disp():
print(x)
x = "Hello World"
disp()
Hello World
Of course, the vice versa is not true. The reason for this is because the local scope of the function is a sub-group within the global scope. Sub-groups may access data from their parent groups, but parent groups cannot access data (variables) from the child groups.
This same logic applies to local scopes within local scopes.
Global variables in Functions
Often you’ll want to create a variable in a function that you can access from the main body of the program. This approach generally isn’t recommended. If you simply want to retrieve a value, it would be better to return
the variable. Or you could you use the classes approach.
This method relies upon the global keyword. When declaring variables in functions, you can add this word as a pre-fix to change it’s scope from local to global. In simple, words you may access that variable from outside the function it was declared in. We’ll use the first example in the local scope section to demonstrate this.
def inc(x):
global result
result = x + 1
inc(5)
print(result)
6
Minimizing Scope
In general, when declaring any variable or constant, it’s good practice to keep the scope as narrow as possible (block scope is the narrowest). This helps conserve memory resources and minimizes the chances of your code referring to the wrong variable by accident.
This marks the end of the Python Variable Scope Tutorial. Any suggestions or contributions are more than welcome at CodersLegacy. Questions regarding the article content can be asked in the comments section below.