Python Global Keyword

Here, you will learn about what is global keyword, rules of global keyword and global keyword across python modules. In python, by using Global...

In this article you will going to learn about the Global Keywords in Python programming language. 

Global Keywords in Python or Python global Keyword

Here, we will discuss about what is Python global keyword, rules of global keyword in python and global keyword across python modules.

But before reading this, you must learn about the Global, Local and Non-local variables in python.

What is Python Global Keyword?

In python, by using Global keyword we can modify a variable outside of its current scope. With the use of global keyword, we can create or declare a global variable inside a function and can make changes in a local context to a global variable. Usually, only local variables can be declared or created inside the function but with the help of global keyword, we can create a global variable inside a function.

Rules of Python Global Keyword

There are some basics rules for using python keyword which are explained as follows;

• If a variable is declared inside a function then it is assumed as local by default.

• If a variable is declared outside a function then by default it is global and we don't have to use the keyword.

keyword is used to declare a global variable inside the function.

• There is no effect of keyword outside the function.

Use of Python Global Keyword

Now, we will discuss how to use global keyword with help of following illustrations;

Illustration no.1

def func():

 global x

x = "Python global Keyword"

print("Printing inside a function: " + x) 

func()

print("Printing outside a function: " + x)

Output

Printing inside a function: Python global Keyword
Printing outside a function: Python global Keyword

In the above example we have used the global keyword to create a global variable inside the function.

Illustration no.2

x = "Python global Keyword"

def func():

 global x

x = "Modified Python global Keyword"

print("Printing inside a function: " + x) 

func()

print("Printing outside a function: " + x)

Output

Printing inside a function: Modified Python global Keyword
Printing outside a function: Modified Python global Keyword

And here, we have used the global scope to modify the value of global keyword inside the local scope.

Global Keyword Across the Python Module

In python, to hold and share the global variable across the different modules, we create a special single module i.e., and after that we have to import the module and then modify the values of that module in and at last we have import both and to test the modification in global variable. 

Let's understand the holding and sharing of global variable across the different modules of python;

Illustration no.3

First of all, Create a python file named with , to store global variables

x = 0
y = "empty"

Create a another file named with , to change global variables

import config

config.x = 14

config.y = "Date of Birth"

At the last, Create a file, to test changes in value

import config

import update

print(config.x)

print(config.y)

Whenever you run on the compiler, you will see the output of something like:

14
Date of Birth

Python Global keyword in Nested Function 

In python, we have to use the global keyword to declare a global variable inside the nested function and if the global keyword is used to modify in the global keyword variable then it will be shown outside the local scope.

Illustration no.4

def func():

    x = 5

    def foo():

        global x

        x = 17

    print("Before change: ", x)

    print("change now")

    foo()

    print("After change: ", x)

func()

print("x in main: ", x)

Output

Before change:  5

change now

After change: 5

x in main: 17

Above, in the Nested foo() function we have declared a global variable. x has no effect on the global keyword inside the func() function.

Conclusion 

From above you have learnt about what is global keyword, rules of global keyword and global keyword across python modules. In python, by using Global keyword we can modify a variable outside of its current scope. In python, we have to use the global keyword to declare a global variable inside the nested function.