Variable scope in Python


Reading time: 5 minutes | Coding time: 2 minutes

The variable scope in Python is the part of a program where a variable is accessible. A variable can be accessed within the region it is defined, we call this it's scope.
Understanding this concept enables a programmer to avoid logical errors when defining functions, naming variables and modifying variables. In this article we'll discuss and demonstrate global and local variable scopes.

Global scope

Variables defined outside any function or code block are global variables and they're available throughout all parts of the program. They form part of the global scope and can be accessed and modified within all functions and code blocks. It is recommended to avoid defining global variables as logical errors could occur and undesired values would be produced if a local variable has the same name as a global variable.

Local scope

Variables defined within a function are local variables and they're available only within that function. They form part of the local scope of a function and may not be accessed or modified from outside this function.

Nested functions

In Python, functions can include other functions within their code. This type of functions are called nested functions. Inner functions are those defined within another function. Outer functions contain inner functions.

Inner and outer functions

Inner functions are functions defined within another function. The local variables of an outer function are available to inner functions, however the inner function's variables are local to that inner function and they're not available to outer functions. It is important to understand this fact when naming and accessing variables so logical errors can be avoided. If a variable inside a function has the same name as a global variable logical errors could occur and undesired values could be produced.

Function parameters

The parameter names of a function contain the values the user pass into that function when it is called. They behave like local variables and are only accessible from within that function.

Variable lifetime

Once variables are created they remain in memory as long as they are being used. Global variables exist in memory as long as that program is executing while local variables exist in memory as long as that function is being executed.

Image

global and local scope

Examples

Global variable

# global variable definition
a = 1

# printing variable a
print(a)

Output:
1

In this example the global variable a is defined and printed. Variable a is outside any function definition and so its is accessible throughout the complete program. The number 1 is the output of this code.

Global and local variables access

# global variable 'a' definition
a = 1

# function definition
def my_function():
    # local variable 'b' definition
    b = 2
    
    # printing of global variable 'a'
    print(a)
    
    # printing of local variable 'b'
    print(b)

# calling the function
my_function()

Output:
1
2

In this example, the function can access and print both the global variable a and its own local variable b. Variable a forms part of the global scope and variable b is local to the function.
By running this code the numbers 1 and 2 are printed vertically on screen.

Nested functions and the global scope

# global variable 'a' definition
a = 1

# outer function
def outer_function(): # outer function
    # outer variable 'b' definition
    b = 2
    
    # printing of variable 'a'
    print(a)
    
    def inner_function(): # inner function
        # inner variable 'c' definition
        c = 3
        
        # printing of global variable 'a'
        print(a)
        
        # printing of outer function variable 'b'
        print(b)
        
        # printing of inner function variable 'c'
        print(c)
    
    # calling inner function
    inner_function()

outer_function()

Output:
1
1
2
3

This example shows a global variable and a function with a nested function. The inner function forms part of both, the the outer function and the global scope. As so, the inner function can access and print all three variables: the global variable a, the outer function's variable b, and its local variable c.
The outer function can access variable a which is global, but it cannot access variable c, as c is local to the inner function. C is defined within the inner function and only the inner function can access the c variable.
By running this code the numbers 1, 1, 2 and 3 are printed vertically on screen.

Question 1

Is a local variable accessible from outside its function?

No
Yes
Depends on data type
Depends on function
A local variable is accessible only within its function, not outside. It forms part of the local scope of that function.

Question 2

Is a global variable accesible within a function?

Yes
No
Depends on data type
Depends on function
A global variable is accesible throughout all parts of a program. It forms part of the global scope.