AppDividend
Latest Code Tutorials

Python Global Keyword Tutorial With Example

0

Python Global Keyword Tutorial With Example is today’s topic. In this post, you’ll learn about the global keyword, global variable and when to use global keywords in Python. In Python, global keyword allows you to modify the variable outside of the current scope. 

Global keyword is used to create the global variable and make changes to a variable in a local context.

Python Global Keyword Tutorial With Example

Content Overview

The basic rules for a global keyword in Python are following.

  1. When we create a variable inside the function, it is local by default.
  2. When we define a variable outside of the function, it’s global by default. You don’t have to use the global keyword.
  3. We use global keyword to read and write a global variable inside the function.
  4. Use of global keyword outside the function does not affect.

Let’s take an example of Global Keyword.

# app.py

def myLocalFunc():
  global site
  site = 'AppDividend'
myLocalFunc()
print(site)

See the output below.

 

Python Global Keyword Tutorial

In the above example, we have defined the Global variable inside the function. Then we have access that variable outside the function.

Due to we have defined the site variable as a Global, we can access that variable outside that function.

Accessing global Variable From Inside a Function

Let’s take an example where we have defined one variable outside the function. Use that variable inside the function and see the output below.

# app.py

site = 'AppDividend'

def myLocalFunc():
    print(site)

myLocalFunc()

The output is following.

 

Python Global Keyword Tutorial With Example

Modifying Global Variable From Inside the Function

In this example, we are defining one variable outside of the function and then change that variable inside the function and see the output of that variable.

# app.py

site = 'AppDividend'

def myLocalFunc():
    site = site + ': A Good Blog'
    print(site)

myLocalFunc()

Now, in the above example, we are trying to modifying that variable which is defined outside the function. Now, that function has no access to the outside variable. We will see an error in the output.

 

Modifying Global Variable From Inside the Function

Changing Global Variable From Inside the Function using Global Keyword

Let’s take an example where we modify the Global variable inside the function using the Global keyword.

# app.py

count = 21

def myLocalFunc():
    global count
    count = count - 2
    print(count)

myLocalFunc()
print(count)

The above program outputs 19 because we are modifying the global variable directly using global keyword.

 

Changing Global Variable From Inside a Function using Global Keyword

Global Variables Across Python Modules

In Python, we create a single module called shared.py to hold the global variables and share information across Python modules within the same program. Let’s take an example.

Now, first in this example, we will need three files.

  1. shared.py
  2. app.py
  3. main.py

Write the following code inside the shared.py file.

# shared.py

count = 21

In this file, we have defined the global variable called count and assign the value 21.

Now, write the following code inside the app.py file.

# app.py

import shared

shared.count = 19

In this file, we have imported the shared file module and modify that variable to 19.

Finally, write the following code inside the main.py file.

# main.py

import shared
import app

print(shared.count)

Run the file by the following command.

python3 main.py

See the output below.

 

Global Variables Across Python Modules

The app.py modifies the global variable, and we have seen in the output.

So, we have seen almost all the use cases of Global keyword in Python.

Finally, Python Global Keyword Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.