GLOBAL VARIALBE
CASE 1:
CASE 1
g=90
def local():
print('inside local',g)
local()
print('global area',g)
output :inside local 90
global area 90
Global var g can be accessed all over the program .inside any function and outside funciton.
CASE 2:
g=90
def local():
print('inside local',g)
g+=10
local()
print('global area',g)
Error: UnboundLocalError: local variable 'g' referenced before assignment
CASE-3
g=90
def local():
global g
print('inside local',g)
g+=10
local()
print('global area',g)
OUTPUT:
inside local 90
global area 100
ITS WORKING SO TO MODIFY GLOBAL VAR INSIDE FUNCTION FUNCTION YOU HAVE TO REDCLARE GLOBAL VAR IN FUNCTION .AS global g
CASE-4
g=90
def local():
global g
print('inside local',g)
g+=10
def local1():
g-=30
print('inside local1',g)
local()
local1()
print('global area',g)
OUTPUT :
ERROR:line 7, in local1
g-=30
UnboundLocalError: local variable 'g' referenced before assignment
Again same Error Because g is modifying in local1,but not declared as global .so all function which modifying global var have reclared as Global.
No comments:
Post a Comment