In this post, we will learn about using global variables in a function. Global variables are those variables that are accessible even outside the body of the program or the function; that is, it is accessible no matter what the scope be. Usually, functions operate within a bound scope with limited access to variables. This comes out as the body of the function. But, setting a variable as global breaks the principles of encapsulation in order that the required variable is more accessible. As we discuss this you can follow this guide on declaring variables in Python.
In Python or in any programming language, generally, any variable is accessible only within a function. The variables having such a scope is the local variable. Thereafter, there are other variables too, which are declared with the global keyword and are hence made global in nature. Thus, they are easily accessible from anywhere in the program. Let’s see what a simple/local variable is in the snippet below; where no value is given as parameters. But, still the variables a and b are ready to display.
def me(): print(a,b) a = "Shubham" b = "Ji" me() # calling the me function to print Shubham Ji print(a,b) # It will print Shubham Ji
These values are ready to be edit as well; however, changes we make within the local environment goes once the execution of the function completes. So, let’s see how to make modifications in the snippet below:
def me(): a = "Ayush" b = "Ji" print(a,b) a = "Shubham" b = "Ji" me() # Ayush Ji print(a,b) # Shubham Ji
The global keyword
To globalize or broaden the scope of any variable, we make use of the global keyword. When we do this, the values of the variable are preserving. Let’s take an example in the snippet below, where a is global and b isn’t. Subsequently, the change to a is there while b retains its original value.
def me(): global a a = "Ayush" b = "Ji" print(a,b) a = "Shubham" b = "Ji" me() # Ayush Ji print(a,b) # Shubham Ji
Global variables are defined and declared outside a function and that we get to use them inside a function.
The Global Scope
def f(): s = " I Am Shubham " print(s) s = " I Love Programming " f() print(s) #Output : I Am Shubham I Love Programming
If a variable with the same name is define inside the scope of the function; then it’ll print the inside value only and not the global one. But, now let’s see another example wherein we modify the value of variable ‘s’ inside the function itself.
def f(): print(s) s = "I Am Shubham." print(s) s = "I Love Programming" f() print(s) # Output : Line 7: undefined: Error: local variable 's' referenced before assignment
To make the above program work, we’d like to use the “global” keyword. Note that the global keyword isn’t a requirement for printing and accessing. Why is it so? Python “assumes” that we would like a variable assignment to s inside f(); therefore, the first print statement throws this error message.
To use the global variable, we’ve to use the keyword “global”.
CONCLUSION — Using global variables in a function
We learnt about variables in Python. Also, we learned about the different definitions of variables on the basis of the scope of the variable. Thereby, after going through the ample number of examples in the snippets above; we are now aware about what a global variable is and how is using global variables in a function done. Hoping in anticipation that this post makes you understand fully. If you have any queries or face any ambiguity; don’t hesitate to comment your doubts. Thereafter, you will get a prompt reply. Until then, see-ya 🙂