Python Global Variable


Python Global Variable

Python Global Variable is a variable that has the scope of whole program. The variable can be accessed anywhere in the program, be it inside or outside of the functions defined in the program.

Example - Global Variable

Following is a simple example of global variable.

Python Program

x = 'hello world - global'

def myFunction():
    print(x)

myFunction()
print(x)

We can access the value of variable x inside myFunction(), and throughout the program. Hence, variable x is global variable.

Output

hello world - global
hello world - global

Global keyword

When you are dealing with functions, you have to be remember some extra points. If you try to update the global variable's value, Python interprets the variable as local variable to the function.

For example, consider the following program.

Python Program

x = 'hello world - 0'

def myFunction():
    x = 'hello world - 1'
    print(x)

myFunction()
print(x)

Output

hello world - 1
hello world - 0

Did you expect the above result! If yes, you already know about global variables. If you did not, let us step ahead and understand why this happened.

The first statement assigns a string literal to the global variable x.

Then we defined a function in which we are trying to change the value of x to some other value.

Now, we call the function, where we think we are updating the value of x. But whats happening in real is that, during execution, a new local variable x is created in the function stack. Global variable x is different from the local variable x. So, any changes that happen to the local variable does not affect the global variable. This is the reason, why we got the local variable's value printed first, and then the global variable value printed out later. Both are different variables and have different memory location, except they share a same name.

Cool! But how do we change the value of a global variable in function. Global keyword comes to the rescue.

If you declare that you are referring to global variable, you can assign a new value to the global variable, inside a function. Now, Python Interpreter does not create a new local variable, but instead uses the global variable.

To declare that we are referring to global variable x inside a function, the syntax is

global x

Let us modify our previous Python program to declare the global variable inside the function.

Python Program

x = 'hello world - 0'

def myFunction():
    global x
    x = 'hello world - 1'
    print(x)

myFunction()
print(x)

Output

hello world - 1
hello world - 1

See! When you call myFunction(), inside the myFunction(), we are telling Python interpreter to use the global variable x. So, the update made to the variable x is made to the global variable x. After the execution of myFunction(), we are printing x. Now, x has an updated value, and the same is printed out.

Summary

In this tutorial of Python Examples, we learned how to define and use a global variable in a Python program.