How to Delete a Global Variable in Python

Introduction

In Python, variables declared outside of the function or in global space are known as global variables. These variables can be accessed by any function in the program. However, there may be instances where you want to delete or change a global variable within a function. This byte will guide you through the process of doing just that.

Global Variables in Python

Before we delve into how to delete or change a global variable, let's take a moment to understand what a global variable is. In Python, a variable declared outside of a function is known as a global variable. This means that the variable can be accessed from anywhere in the code - be it within a function or outside.

Here's a simple example of a global variable:

x = 10

def print_global():
    print("The global variable is: ", x)

print_global()

Output:

The global variable is:  10

Here, x is a global variable because it is defined outside of the print_global function, yet it can still be accessed within the function.

Why Delete a Global Variable?

So you might be wondering, why would we ever want to delete a global variable? Well, in large programs, global variables can consume significant memory resources, especially if they contain large data structures or objects. Or maybe your list of global variables (via globals()) has become far too cluttered.

One advantage of deleting global variables you no longer need is that it can help to free up memory, improving the footprint and efficiency of your code.

Wait! Deleting a global variable should be done with caution, as it can lead to errors if other parts of your program are still trying to access it.

How to Delete a Global Variable

Deleting a global variable in Python is pretty easy to do - we just use the del keyword. However, if you try to delete a global variable directly within a function, you will encounter an error. This is because Python treats variables as local by default within functions.

Here's an example:

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

x = 10

def delete_global():
    del x
    print(x)

delete_global()

This will output:

UnboundLocalError: local variable 'x' referenced before assignment

To delete the global variable within the function, we need to declare it as global within the function using the global keyword:

x = 10

def delete_global():
    global x
    del x

delete_global()

print(x)

This will output:

NameError: name 'x' is not defined

As you can see, after calling the delete_global function, trying to print x results in a NameError because the global variable x has been deleted.

Conclusion

In this Byte, we've learned about global variables in Python and why you might want to delete them. We've also seen how to delete a global variable within a function using the del and global keywords. Just always make sure that the variable is not needed elsewhere in your code before deleting it.

Last Updated: September 15th, 2023
Was this helpful?
Project

Building Your First Convolutional Neural Network With Keras

# python# artificial intelligence# machine learning# tensorflow

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup
Details

Ā© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms