Tuesday, February 2, 2021

Global Keyword in Python With Examples

In this post we’ll see what is global keyword in Python and how to use it.

To understand global keyword better an understanding of local, global and nonlocal variables is required, as a requisite please go through this post- Local, Nonlocal And Global Variables in Python

Some important points that you should know about local and global variables in Python-

  1. Variables that are declared outside a function are known as global variables in Python. There is no need to qualify these variables with global keyword such variables have global scope by default.
  2. When a variable is declared inside a function then it is a local variable. A local variable’s scope is limited with in the function where it is created.
  3. If you declare a variable inside a function having the same name as the global variable then with in the scope of the method local variable overshadows the global variable.

Python global keyword

When you want to use a global variable inside a function you can qualify a variable with global keyword inside the function body.

For example-

def myfunction():
  global x
  .....
  ..... 

Using global keyword in Python

The third point mentioned above “If you declare a variable inside a function having the same name as the global variable then with in the scope of the method local variable overshadows the global variable.” is the basis for using global keyword in Python. To understand it let’s go through few scenarios.

Variable with same name in both global and local scope

#global variable
x = 10
def myfunction():
  # local variable
  x = 7
  print('x inside function', x)

myfunction()
print('x outside function', x)

Output

x inside function 7
x outside function 10

As you can see local variable x inside the function overshadows the global variable x with in the limit of the function. Outside the function scope changes to global variable.

Modifying global variable with in a function

You may have a scenario where you’d want to modify global variable with in a function.

#global variable
x = 10
def myfunction():
  #modify global variable
  x = x+2
  print('x inside function', x)

myfunction()
print('x outside function', x)

Output

  File "F:/NETJS/NetJS_2017/Python/Test/HelloWorld.py", line 8, in <module>
    myfunction()
  File "F:/NETJS/NetJS_2017/Python/Test/HelloWorld.py", line 5, in myfunction
    x = x+2
UnboundLocalError: local variable 'x' referenced before assignment

As you can see trying to modify global variable with in the function results in an error ‘UnboundLocalError: local variable 'x' referenced before assignment’ because Python looks for variable x with in the function which is not found thus the error.

Examples using global keyword in Python

So we have seen the scenarios where trying to access global variable with in the method is required and that’s where Python global keyword is used.

Modifying global variable with in a function

If you want to modify global variable inside a function then the variable needs to be declared using global keyword with in the function. That way you can modify global variable with in the local context.

#global variable
x = 10
def myfunction():
  global x
  #modify global variable
  x = x+2
  print('x inside function', x)

myfunction()
print('x outside function', x)

Output

x inside function 12
x outside function 12

As you can see now global variable can be changed with in the function.

Creating a global variable from a local scope

By qualifying a variable with global keyword inside a function you can create a global variable inside a function that can be used outside the function too.

def myfunction():
  # global variable from local scope
  global x
  x = 7
  print('x inside function', x)

myfunction()
print('x outside function', x)

Output

x inside function 7
x outside function 7

That's all for this topic Global Keyword in Python With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Nonlocal Keyword in Python With Examples
  2. Namespace And Variable Scope in Python
  3. Strings in Python With Method Examples
  4. Multiple Inheritance in Python
  5. Method Overloading in Python

You may also like-

  1. Class And Object in Python
  2. Passing Object of The Class as Parameter in Python
  3. User-defined Exceptions in Python
  4. Convert String to float in Python
  5. How to Format Date in Java Using SimpleDateFormat
  6. Byte Streams in Java IO
  7. Circular Dependency in Spring Framework
  8. Sequence File in Hadoop