Python Global Variables(with examples)

Python Global Variables

Hello Techies,

This blog is based on Python Global Variables. In this blog, you will learn about the use of Python global variables with the help of multiple examples.

Below are the points that we are covering in this blog:

  • What are Global variables in Python?
  • How do you create Global Variables in Python?
  • Python global variables across modules

What are Global variables in Python?

When a variable is declared at the top of a function, they become a global variable. These variables are available in all the functions written later.

We can use global variables throughout the program. We can use it inside or outside the function. The scope of the global variable is the entire programming body written below.

Key Points of Python Global Variables

  • You can access the Python global variables from all the functions or modules in the program.
  • Python global variables are useful when multiple functions are accessing the same data.
  • It provides data sharing.

How do you create Global Variables in Python?

Let us see an example of how to create Global variables in Python.

x = "global"

def my_func():
    print("Inside Value:", x)

my_func()
print("Outside Value:", x)

Output is:

Inside Value: global
Outside Value: global

In the code above, we create an x as a global variable and define a my_func() function to print the global variable x. Finally, we call the my_func() function which will print the value of x.

What if you want to create the same name variable in the function?

If you create a variable with the same name in the function, this variable will be local and can only be used within the function. The global variable of the same name will remain the same as the global and original value.

x = "global"

def myfunc():
  x = "Python"
  print("Inside Value:", x)

myfunc()

print("Outside Value:", x)

Output is:

Inside Value: Python
Outside Value: global 

What if you want to change the value of x which define inside the my_func() function?

x = "global"

def myfunc():
  x = x + "value"
  print(x)

myfunc()

Output is:

UnboundLocalError: local variable 'x' referenced before assignment

The output shows an error because Python treats x as a local variable and x is not defined in the my_func() function.

Use global keyword to solve this type of Error. Using global keyword you can change a global variable inside a function.

Example: How do you change Global Variables in Python?

x = "global"

def myfunc():
  global x
  x = x + " value"
  print("Inside Value:", x)

myfunc()
print("Outside Value:", x)

Output is:

Inside Value: global value 
Outside Value: global value

As we can see, When we modify the global value in the my_func() function, the global variables outside the function also change.


Python global variables across modules

In Python, if you want to share global variables across modules in the same program, create a config.py or constant.py file and import this file into all modules of your application; The module will then be available as a global name.

Check out the following example of how we can share global variables on the Python module.

Example:

Create a config.py or constant.py file, to store global variables.

x = 15
b = "Python"

Let’s create a main.py file to check the change in value.

import config #If the file name is constant.py, rename it accordingly

print("Original value of x is: ", config.x)
print("Original value of y is: ", config.y)

print("After modification value of x is: ", config.x + 10)
print("After modification value of y is: ",  config.y + " is easy to learn")

Output is:

Original value of x is:  15
Original value of y is:  Python
After modification value of x is:  25
After modification value of y is:  Python is easy to learn

See the official site for more information.


FAQ on Python Global Variables

  1. What is meant by global variable in python?

    When a variable is declared at the top of a function or in global scope, they become global variables. This means we can use global variables throughout the program.

  2. How do you declare a global variable in python?

    1) Create a global variable outside the function.
    2) When you want to access the same global variable in a
    function, declared it using a global keyword.

  3. Why are global variables bad?

    Using global variables in a program is a bad practice because they can be used simultaneously across multiple sections of the program, and this often results in bugs.

Leave a Comment