Global Variables In Python – POFTUT

Global Variables In Python


Python provides the Global and Local variables in order to use the same variable identifier in different code blocks. Global variables are generally defined outside of the functions where they can be used everywhere in the code file. But global variables can be also used inside the function too.

Variable Scope

Before starting to explain the global variable and creation we need to learn the variable scope in Python. Variables defined outside functions and classes are by default global variables. They can be used inside the applications from everywhere except function body or scope and class body or scope.

#Global Scope
global_var=5

def myf():
   #Local Scope
   local_var = 4

#Global Scope
print(global_var)

In the example above we can see that the variable named local_var is defined inside the function. The local_var is only accessible inside the local scope of the function. The variable named global_var is defined in the global and can be used in the global scope by default.

Define Global Variable

Let’s start with a simple example where we will define a global variable and use this variable multiple times in the global scope. Also, we will try to use this global variable inside a local scope which will not work.

#Global Scope
a = 5

def myf():
   # This will print 5 
   print(a)

myf()

# This will print 5
print(a)

We can see from the example that there is a single variable that is global and named a. When we use this global variable inside a local scope we can access it without a problem and the value of the global variable is printed.

Define Local Variable

Local variables are defined inside the local scope and only accessible from the current local scope. If there is a global variable named with the same name they will behave like different variables. In the following example, we will define two variables with the same name which is a, and we will print their values.

# Global Scope
a = 5

def myf():

   # Local Scope
   a =4
    
   # This will print 4
   print(a)

myf()

# This will print 5
print(a)

global Keyword

global keyword is used to access a global variable that is outside of the current local scope. For example, a global variable can be accessed inside a function by using a global keyword. global keyword is only used one time to make global variables accessible from the local scope. Below are some rules about the global keyword.

  • If the variable is used inside the local scope like assigning a value, it is assumed as a local variable unless using the global keyword.
  • There is no need to use the global keyword outside of the local scope.
  • If a variable name is used as a local variable it can not be made global with the global keyword.
# Global Scope
a: int = 5

def myf():

    global a

    # This will print 5
    print(a)

myf()

# This will print 5
print(a)

When we try to use the already used local variable name we will get an error like “SyntaxError: name ‘a’ assigned to before global declaration”.

Global Variable Error
# Global Scope
a: int = 5

def myf():
    # Local Scope
    a = 4

    global a

    # This will print 5
    print(a)

myf()

# This will print 5
print(a)

LEARN MORE  How To Learn Python Programming?

Leave a Comment