How to Use Global Variables in Python Classes?

Recently, a New York Python user group member asked me about using global variables in Python classes. In this tutorial, I will explain how to use global variables in Python classes with examples. I will provide practical examples of how to declare, access, change, pass, and set global variables in Python classes.

Declare a Global Variable in a Python Class

To declare a global variable in Python, you simply define it outside of any function or class. This makes the variable accessible throughout the entire module.

Here is an example and the complete Python code.

# Global variable
global_var = "Hello, USA!"

class MyClass:
    def __init__(self):
        pass

    def print_global(self):
        print(global_var)

# Usage
my_obj = MyClass()
my_obj.print_global()  # Output: Hello, USA!

In the example above, global_var is declared outside the class and is accessible from within the class method print_global.

I executed the above Python code, and you can see the output in the screenshot below:

how to declare global variable in python class

Read Asterisks (*) in Python

Access a Global Variable in a Python Class

Now, let me show you how to access a global variable in a Python class. You can directly use the global variable inside any method of the class in Python.

Here is an example.

global_var = "Hello, New York!"

class MyClass:
    def __init__(self):
        pass

    def access_global(self):
        print(f"The global variable is: {global_var}")

# Usage
my_obj = MyClass()
my_obj.access_global()  # Output: The global variable is: Hello, New York!

In this example, the access_global method prints the value of the global variable global_var.

Here is the output in the screenshot below:

how to access global variable in python class

Check out How to Make a Variable Global in Python?

Change a Global Variable in a Python Class

To change the value of a global variable from within a class, you need to use the global keyword. This keyword tells Python that you are referring to the global variable, not a local one.

Here is how you can do this, follow the below Python code.

global_var = "Hello, California!"

class MyClass:
    def __init__(self):
        pass

    def change_global(self):
        global global_var
        global_var = "Hello, Texas!"

# Usage
my_obj = MyClass()
my_obj.change_global()
print(global_var)  # Output: Hello, Texas!

Here, the change_global method modifies the value of global_var using the global keyword. Once you execute the above code, you can see the exact output like in the screenshot below:

how to change global variable in python class

Pass a Global Variable in a Python Class

You can also pass a global variable to class methods as a parameter in Python. This approach can be useful when you want to avoid using the global keyword.

Here is how to do it. Follow the Python code below.

global_var = "Hello, Chicago!"

class MyClass:
    def __init__(self):
        pass

    def use_global(self, var):
        print(f"Using the global variable: {var}")

# Usage
my_obj = MyClass()
my_obj.use_global(global_var)  # Output: Using the global variable: Hello, Chicago!

In this example, the global variable global_var is passed to the use_global method as an argument.

Here is the output in the screenshot below:

how to pass global variable in python class

Read How to Check if a Variable is Global in Python?

Set a Global Variable in a Python Class

Now, let me show you how to set global variables in a Python class.

Setting a global variable from within a class is similar to changing it. You use the global keyword to ensure that the variable is recognized as a global variable.

Here is the complete Python code.

global_var = "Hello, Miami!"

class MyClass:
    def __init__(self):
        pass

    def set_global(self, new_value):
        global global_var
        global_var = new_value

# Usage
my_obj = MyClass()
my_obj.set_global("Hello, Los Angeles!")
print(global_var)  # Output: Hello, Los Angeles!

In this example, the set_global method sets the value of global_var to a new value passed as an argument.

Here is the output in the screenshot below:

Set a Global Variable in a Python Class

Conclusion

In this tutorial, I covered how to declare, access, change, pass, and set global variables in Python classes. I hope you now know how to use global variables in Python classes.

You may like the following tutorials:

Leave a Comment