Use of Global Variables in Functions

Home /

Table of Contents

Global Variable in Python

A global variable is a variable that is defined outside of a function and can be accessed from any function within the program in python. When a variable is defined within a function, it is considered a local variable and can only be accessed within that function. Global variables can be useful in situations where a value needs to be shared across multiple functions or when a value needs to be updated by multiple functions. However, it is generally considered best practice to avoid the use of global variables in favor of passing values as function arguments, as this can make the code more readable and easier to debug.

Global Variable outside of a function

In Python, you can create a global variable by defining it outside of any function. For example:

Python
x = 5

def my_function():
    print(x)

my_function()  # prints 5

In this example, ‘x‘ is a global variable that is defined before the function ‘my_function', and it can be accessed within that function.

Global Variable within a Function

You can also use the ‘global‘ keyword within a function to indicate that a variable is a global variable, even if it is defined within a function.

Python
def my_function():
    global x
    x = 5

my_function()
print(x)  # prints 5

However, it is generally considered best practice to avoid the use of global variables in favor of passing values as function arguments.

Global Variables for Configartion Values

Global variables can be used to store configuration values that are used throughout a program. Here’s an example of how to use a global variable for configuration values:

Python
# define a global variable for the maximum number of retries
MAX_RETRIES = 3

# define a function that uses the global variable
def make_request(url):
    for i in range(MAX_RETRIES):
        # make the request
        response = requests.get(url)
        if response.status_code == 200:
            # success
            return response.text
        else:
            # retry
            time.sleep(1)
    # maximum retries exceeded
    return None

In this example, the global variable ‘MAX_RETRIES‘ is defined at the top level of the module and can be accessed by any function in the module. The ‘make_request()‘ function uses the global variable to control the number of retries for a network request.

By using a global variable for the configuration value, you can easily change the value in a single place and have it propagate throughout the program. For example, if you wanted to change the maximum number of retries to 5, you could simply change the value of the ‘MAX_RETRIES‘ variable at the top level of the module:

Python
# change the maximum number of retries to 5
MAX_RETRIES = 5

This would affect the behavior of the ‘make_request()‘ function and any other functions that use the ‘MAX_RETRIES‘ variable.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article