Scopes & Global Variables in Python – Explained

Python Scopes & Global Variables

This article will explain and demonstrate scopes in Python – including the global scope and how to declare global variables.

What is a Scope?

As you move towards building more complex Python applications, you’ll be using functions, loops, try/except statements for error handling, and other more advanced constructs.

Which variables are available within each of these constructs is defined by the variable’s scope.

The scope is where the variable is available within your program.

Different variables have different scopes, and totally distinct/separate variables in different scopes can share names, so it’s worth having a good idea of how scopes work so that you can spot code errors when something goes wrong.

Python Scopes

Scopes in Python can either be local or global.

Global Scope

A variable declared in the global scope is available everywhere in the main Python program.

What About Modules?

Python modules each have their own individual global scopes. Therefore, if you are importing code as a module, global variables from within the module will not be available in your main program or each other.

The Local Scope

Variables declared in local scopes are only available within that scope. Local scopes include variables declared inside functions and classes.

Local variables declared within functions only exist for as long as it takes for the function to execute and are then discarded.

Global Scope & Declaring Global Variables from Other Scopes

Global variables can be specifically referenced or created from within local scopes using the global keyword.

Demonstration & Example

The easiest way to show how scopes work is with some code. The below example includes variables declared in a variety of scopes, with comments explaining which scope the variable has been declared in and why:

# Declare a global variable
# This variable is declared outside of a function, loop or class in the main program
myGlobal = 6

# Declare a new function
def myFunction():

    # Declare a local variable
    # This variable is declared inside of a function and is only available inside the function
    # Each time the function is run a new namespace/scope is created - modifying the variable in one run of the function will not affect it when the function is used again
    myVariable = 3

    # Global variables are available inside functions
    print(myGlobal)

    # The value of a global variable can be changed within the function, but it will only apply within the current execution of the function - the change will not be reflected elsewhere
    # Effectively, it's creating a new local variable with the same name of the global variable which will be used instead
    myGlobal = 8

    # To update the global variable from within the function, use the global keyword to reference it
    global myGlobal
    myGlobal = 8

    # This can also be used to create new global variables from within a function
    global myNewGlobal = 12

    # Global variables can be updated from within loops without using the global keyword
    count = 1
    while count < 5:
        # Print and increment the global counter
        print(count)
        count += count

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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