How to Modify Python Global Variable inside a Function

How to Modify Python Global Variable inside a Function

Yes, We can modify a global variable from within a function using the “global” keyword. We just have to declare the variable again inside the function as “global variable_name

variable_name = "Learn & Share"

def my_function():
	global variable_name
	print(variable_name)

#Calling the function
my_function()

Let’s talk in more detail about modifying the global variable and scope in Python.

Scope in Python

The scope is basically a concept of restricting or enabling the use of variables across the program and there are two types of scope

  • Global Scope
  • Local Scope

Now, We will see, How to Modify the Global Variable

Global variable falls under the global scope, So printing or editing the global variable within a function is restricted

Steps to declare and modify the global variable

An example that won’t work:

water = 1
def add_water():
  water = 2
  print ("Total liters of water in the inside tank: " , water)

add_water()
print ("Total liters of water in the Outside tank: " , water)

Letโ€™s check about the above example,

  • water” outside of the function is called the “global variable” and inside the function is called the “Local variable
  • In the above code, editing the local variable will not going to impact the global variable

Let’s see the output:

Total liters of water in the inside tank:  2
Total liters of water in the Outside tank:  1

So the Outside tank will still have 1 liter of water ๐Ÿ™‚

So, To modify the global variable inside a function, we just had to use the “global” keyword

An example that will work:

water = 1
def add_water():
  global water
  water = 2 
  print ("Total liters of water in the inside tank: " , water)

add_water()
print ("Total liters of water in the Outside tank: " , water)

As we have declared the variable again in the function with the keyword “global”, Now function “add_water” can able to edit the variable “water”

Let’s see the output now

Total liters of water in the Inside tank: 2
Total liters of water in the Outside tank: 2

Great! We have learned to modify a global variable.

Quick Tips on How and why to avoid modifying the global variable

In real world program, There will be multiple teams involved in creating a project. Modifying a global variable inside a function will make it difficult to remember the reason for the use or even its existence. So it would be efficient to avoid changing it inside a function ๐Ÿ™‚

Modified example:

water = 1

def add_water():
  return water + 1 

water=add_water()
print ("Total liters of water in the Outside tank: " , water)

Output:

Total liters of water in the Outside tank:  2
  • We have managed to edit the code to avoid modifying the global variable inside the function by just returning the results
  • Now, We don’t have to worry about the function and what it is doing and it can be placed anywhere in the program. All we need to know is to call the function add_water(), If we need to add water.

NOTE: The code written here is optimized to work in this editor. If you want to try it in your local IDE. You need to do a few syntax corrections. Comment or chat with us for any questions

Read More:

ImportError: No module named pip

Python try except print error

Easiest Way to Connect Hive with Python:PyHive

Things to Know Before Learning Python

How to Convert JSON to YAML

Fix โ€“ attributeerror: module numpy has no attribute int

Jerry Richard R
Follow me