How to Check if a Variable is Global in Python?

Variables can be local or global, but as a Python developer, you should know the scope of the variable before using it. In this tutorial, I will explain how to check if a variable is global in Python with examples and complete code.

To check if a variable is global in Python, you can use the globals() function. This function returns a dictionary of the current global symbol table, which includes all global variables. Simply check if the variable name exists in this dictionary. For example, if 'variable_name' in globals(): will return True if variable_name is a global variable, and False otherwise.

What is a Global Variable in Python?

A global variable is one that is defined outside of any function and is accessible throughout the entire program. In contrast, a local variable is defined within a function and is only accessible within that function.

Here’s a simple example to see the difference:

# Global variable
city = "New York"

def print_city():
    # Local variable
    city = "San Francisco"
    print("Local city:", city)

print_city()
print("Global city:", city)

Output:

Local city: San Francisco
Global city: New York

In this example, city is a global variable when accessed outside the function and a local variable when accessed inside the function.

If you are new, then check out Local and Global Variables in Python and How to Make a Variable Global in Python?

Check if a Variable is Global in Python

To check if a variable is global, you can use the globals() function. This function returns a dictionary representing the current global symbol table, which includes all global variables.

Here’s how you can use globals() to check if a variable is global:

# Global variable
state = "California"

def check_variable(var_name):
    if var_name in globals():
        print(f"{var_name} is a global variable.")
    else:
        print(f"{var_name} is not a global variable.")

check_variable('state')
check_variable('city')

Output:

state is a global variable.
city is not a global variable.

In this example, state is a global variable, and city is not defined globally, so the function correctly identifies their scopes.

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

Check if a Variable is Global in Python

Using the global Keyword

The global keyword in Python is used to declare that a variable inside a function is global. This is useful when you need to modify a global variable inside a function.

Here’s an example:

# Global variable
country = "USA"

def change_country():
    global country
    country = "Canada"

print("Before change:", country)
change_country()
print("After change:", country)

Output:

Before change: USA
After change: Canada

In this example, using the global keyword allows the change_country function to modify the global variable country.

Conclusion

In this tutorial, I have explained how to check if a variable is global in Python. By using the globals() function and the global keyword, you can easily check if a variable is global in Python. If these examples help you, leave a comment below.

You may like the following tutorials:

Leave a Comment