You are currently viewing Python Global Variables in Function

You will discover how to use global variables in Python functions. In Python, variables that can be accessed from different parts of your code are known as global variables. They can be declared outside of a function or within a function using the global keyword. In this article, we will explore the concept of global variables in Python functions.

Advertisements

1. What is a Global Variable?

A global variable is a variable that can be accessed and modified from any part of a Python program, regardless of where it was defined. In other words, a global variable is a variable that is defined outside of any function or class and is therefore available for use throughout the entire program.

2. Properties of Global Variables

Global variables are useful in situations where you need to store data that is used across multiple functions or modules. For example, you might use a global variable to store configuration settings for your program that need to be accessed by several different functions.

  • They can be accessed and modified from any part of a program.
  • They are defined outside of any function or class.
  • They have a global scope, meaning they are available to all parts of the program.

3. Syntax of Global Variable

To define a global variable in Python, you can use the following syntax:


# Syntax of defining global variable
global variable_name

It’s important to note that the global keyword should be used when you wanted to define a global variable inside a function. Also, note that the global keyword is not required to specify when you wanted to define outside of the function as by default it considers a global variable.


# Glonal variable inside a function
def my_function():
    global my_variable
    my_variable = 23

When you use the global keyword inside a function, you are telling Python that the variable should be considered global, and that any changes made to it inside the function should affect the global variable.

4. Global Variable in Function

Local variables are defined within the scope of a function and cannot be accessed outside of it. Global variables, on the other hand, are defined outside of any function and can be accessed by any function within the python program. They have a global scope and are visible throughout the program.

When a function modifies a global variable, the change is visible to any other function that accesses that variable.

Most important point is that variable that is declared outside the function is a global variable and doesn’t need any global keyword.

Here is an example of defining a global variable in Python:


# global variable
global_var = 10 

def my_function():
    # accessing global variable inside the function
    print(global_var) 

my_function() 

# Outputs:
# 10

If you have the intention to declare the global variable without the initialization, then you need to put the global keyword before it. See the following example.


# global variable 
global_var  = 5

def my_function():
    # updating the global variable
    global global_var
    global_var = 20 
    print(global_var)

my_function() 
print(global_var)

# Output:
# 20

5. How Python Handles Global Variables in Functions

Whenever a variable is defined inside a function, it is considered a local variable by default. This means that it can only be accessed within that function and cannot be used outside of it.

When a variable is referenced within a function, Python first looks for it within the local namespace of the function. If it is not found, Python then looks for it in the global namespace.

If the variable is not found in either namespace, Python will raise an error. This process of searching for variables is known as the “LEGB” (Local, Enclosing, Global, and Built-in) rule in Python.

In cases where a function needs to modify the value of a global variable, the “global” keyword can be used to indicate that the variable should be treated as a global variable rather than a local variable.

6. Local and Global Variables Naming Conflicts in Function

There can be a variable naming conflict if you don’t use the global and local variables properly. below are few examples where you can have a naming conflict.

Example No 1:


global_var = "I am global"

def my_function():
    local_var = "I am local"
    print(local_var)

my_function()
print(global_var)

# Output:
# I am local
# I am global

Example No 2 :


global_var = "I am global"

def my_function():
    global_var = "I am local"
    print(global_var)

my_function()
print(global_var)

# Output:
# I am local
# I am global

Example No 3:


global_var = "I am global"

def my_function(global_var):
    print(global_var)

my_function("I am local")
print(global_var)

# Output:
# I am local
# I am global

7. Summary and Conclusion

Global variables can be a powerful tool in Python programming, but they must be used with care to avoid potential issues such as naming conflicts and unintended side effects. We have discussed how to use global variables in Python functions with examples. If you have any questions please comment.

Happy coding!