Python global statement uses and drawback

The Python global statement sole purpose is to make any object or variable have a global scope. If any local scope object is declared with the global statement, then the object automatically becomes a global scope object.

Link : Python scopes type

Some points to note while using global statement.

i)A global variable does not require declaring it with the ‘global’ stamtent

A global variable declared outside all the scopes has a global scope, so declaraing it with the global statement is meaningless.

>>> gl=90 # global variable
>>> global gl1 #Work fine but 'global' is not necessary
>>> gl1=10 

ii)Inside the local scope we can assign value to a global variable using only the global statement

Inside any function, if you want to change the value of a global variable, we can do so by redeclaring the variable with the ‘global’ statement. Changing the value without redeclaring it with the global statement has no effect on the global variable.

>>> #Changing the value without the global statement
>>> gb=900 #global variable
>>> def func():
	gb=999

	
>>> func()
>>> gb
900

Inside the function we have assigned 999 to gb but the value of the global variable ‘gb’ is not changed. Note the gb declared inside the function is a different variable having different id and its scope is of local scope type.

In the example below, we use the ‘global’ statement to change the value of the global variable.

>>> gh=999
>>> def foo():
	global gh
	gh=1234

>>> foo()
>>> gh
1234

The gh value has been changed to 1234, which is the value assigned to gh inside the function.



iii)You cannot declare a variable with the ‘global’ if there is any preceding declaration of the variable

Suppose a variable has been declared, we cannot use the same name of that variable to declare it as ‘global’ again if the two names exist in the same block. By the ‘same block’, we mean the scope or region of the program.

Consider the program below.

>>> def fun():
	kk=89
	global kk

The code above will give an error “SyntaxError: name ‘kk’ is used prior to global declaration“. The name kk is already declared in the local region, so redeclaring it with the ‘global’ statement renders the error.

If the variable are in different block or scope we can redeclare it with the ‘global’ statement.

>>> def fu():
	jj=90
	def fu1():
		global jj
		jj=890
		print('jj=',jj)
	fu1()
	print('\njj=',jj)

The first ‘jj’ lies in the nested scope and the second jj is in the local scope. Both of them are in different block so the above code works fine.


iv)The global statement is not allowed with the parameter declaration or inside the for loop or class definition or import statement.

The ‘global’ statement cannot be used in any of the cases:
i)parameter declaration

ii)inside the for loop

iii)class definition

iv)import statement

v)Function definition.

>>> def func( global k):
...
SyntaxError: invalid syntax
>>> ls=[23 ,89 , 90]
>>> for global a in ls:
...
SyntaxError: invalid syntax

v)The global statement is necessary if any global variable is to be deleted in the local scope

If you want to delete a global variable inside a local scope, you must declare it with the ‘global’ statement first. If not you will get an error.

>>> hj=890
>>> def func():
	del hj

	
>>> func()

Calling func() will most likely give you an error ‘UnboundLocalError: local variable ‘hj’ referenced before assignment‘. To avoid this redeclare the ‘hj’ variable with the ‘global’ statement.

def fuu():
	global hj
	del hj

	
>>> fuu()
>>> hj

Since hj has been deleted, acessing hj will give the error “NameError: name ‘hj’ is not defined“.

Link : Python del statement


Drawback of ‘global’ statement

The main drawback of ‘global’ statement lies in the fact that, a global object can be changed from anywhere inside the program. This flexibility of global variable may sometime render an unexpected output. Consider the code below.

>>> radius=10
>>> def area(rad):
	global radius
	radius=rad
	return 2*3.1415*radius

>>> print("Radius=",radius)
Radius= 10
>>> print("Area=",area(23) )
Area= 144.50900000000001
>>> print('Radius=',radius)
Radius= 23

The ‘radius’ value is easily changed.


Conclusion

The nonlocal statement is a counterpart of the global statement for enclosed scope.

Link : Python nonlocal statement




Leave a Reply

Your email address will not be published. Required fields are marked *