Monday, December 7, 2020

Global Keyword in Python and its usage

 

Global Keyword in Python

Sometimes, the global variables and the local variables have the same name. In such a case, the function by default refers to the local variable and ignores the global variable. Therefore, the global variable is not accessible inside the function but it is present outside the function where it is globally accessible.

 Program

# Python program to demonstrate the concept of  global and local variables, same 

# name used for  global and local variables in the sample code


var = 10 # this is global variable

def somefunction():

var = 20 # this is a local variable

print(' value of variable =',var) # local var


somefunction()

print(' variable value =',var)


 

Output

value of variable = 20

variable value = 10

 

When the programmer/user wants to use the global variables inside a function, he can use the keyword 'global' before the variable in the beginning of the function body.

  

For example :

global var

 

In the given programmatic method, the global

variable is made available to the function and the

programmer can work with it in whichever manner the programmer/user wishes to work with the provided object or variable .In the following program, a method to see how to

work with a global variable inside a function.

 

==================================

Program

A Python program to access global variables inside a function and modify the values .

 

# accessing a global variable inside a function

var = 10                                                                  # this is a global variable

def somefunction():

global var                                                   # this is a global variable

print(' global variable = ',var)              # display global variable

var = 20                                                       # modify global var value

print(' modified variable value = ', var)

 

somefunction()

print(' global variable value = ',var)             # display modified value

 

Output

global variable = 10

modified variable value = 20

global variable value = 20

 

When an issue arises when the global variable name and local variable names are same , the programmer/reviewer/user  will face difficulty to differentiate between them inside a function .

 

For example, if there is a global variable 'a' with some value declared above the function.... Then there is a global variable 'a' with some value declared above the function. Here, the programmer  is writing a local variable with the same name 'var' with some other values inside the function .

 

Consider the following code :

var = 10                                 # this is a global variable

def myfunction():

var = 20                       # this is a local variable

 

In a scenario, when the programmer wants to work with a global variable, then the programmer would use a 'global' keyword . Then the programmer can access only the global variable and the local variable is no more available .

The globals() function can be used to solve this problem of using global variable which would solve the problem .This is a built-in function which returns a table of current global variables in the form of a dictionary.

 

Program

# Python program to get a copy of global variable into a

# function and work with it

 

# same name for global and local variable

 

var = 10                                                       # this is a global variable

def myfunction():

var = 20                                           # this var is a local variable

x = globals()['var']

print('global variable var = ', x)

print(' local variable var = ',var)

myfunction()

print(' global variable value =',var)

 

Output

global variable var = 10

local variable var = 20

global variable var = 10

No comments:

Post a Comment