September 11, 2024

How to Declare a Global Variable in Python

In Python, variables declared outside of a function or in the global scope are considered global variables. These variables can be accessed and modified from any part of the code, including inside functions. However, if you want to modify a global variable inside a function, you need to explicitly declare it as global using the global keyword. Below are examples of how to declare and use global variables in Python.

1. Declaring a Global Variable

To declare a global variable, simply assign a value to a variable outside of any function. This variable can be accessed from anywhere in the code.

Example:

# Declaring a global variable
global_var = 10

def print_global_var():
    print("Global variable:", global_var)

print_global_var()  # Accessing the global variable from a function

Output:

Global variable: 10

In this example, global_var is a global variable and is accessible inside the print_global_var() function.

2. Modifying a Global Variable Inside a Function

If you want to modify the value of a global variable inside a function, you need to use the global keyword to declare that you are referring to the global variable, not a local one.

Example:

# Modifying a global variable inside a function
global_var = 10

def modify_global_var():
    global global_var
    global_var = 20
    print("Global variable inside function:", global_var)

modify_global_var()
print("Global variable outside function:", global_var)

Output:

Global variable inside function: 20
Global variable outside function: 20

In this example, the global_var is modified inside the modify_global_var() function, and the change is reflected outside the function as well.

3. Using Global Variables Across Multiple Functions

Global variables can be accessed and modified by multiple functions within the same program.

Example:

# Using a global variable across multiple functions
counter = 0

def increment_counter():
    global counter
    counter += 1

def print_counter():
    print("Counter value:", counter)

increment_counter()
increment_counter()
print_counter()  # Output should be 2

Output:

Counter value: 2

In this example, the counter global variable is incremented in the increment_counter() function and accessed in the print_counter() function.

4. Potential Issues with Global Variables

While global variables can be convenient, they can also lead to issues such as unintended side effects and difficulties in debugging. It’s generally a good practice to limit the use of global variables and prefer passing variables as function arguments.

Example of Potential Issue:

# Potential issue with global variables
def function1():
    global var
    var = "Changed in function1"

def function2():
    print("Value of var in function2:", var)

var = "Initial Value"
function1()
function2()  # Output will be "Changed in function1"

Output:

Value of var in function2: Changed in function1

In this example, the global variable var is modified in function1(), which affects its value in function2(). This can lead to unexpected behavior if not carefully managed.