In this tutorial we will be discussing about Python global variables.
A variable which is declared outside of any function i.e. in the global space is known as global variable.
Global variables are useful when multiple functions need to use the same data. In C, C++ we can use and modify global variables directly in local functions and if we declare a local variable with the same name of global variable then it shadows the global variable but this is not the case in Python.
Let’s see this with the help of one example :
def foo(): print(x) x = 5 foo() print(x)
The output of the above program is:
5
5
Here x is global variable and it can be accessed in function and the program works correctly but what if we try to change the variable value inside the function.
def foo(): x = x+2 print(x) x = 5 foo() print(x)
When we run the above code, we will get the below error:
UnboundLocalError: local variable ‘x’ referenced before assignment
Here the problem is x in the function is now being treated as a local variable.
In python if it is just reading the variable and the variable doesn’t exist locally then it will try to look into any containing scope (Global scope in our first example). But this is not the case when we are trying to change the variable value. Python assumes that any variable whose value is being changed in any function is local to the function unless explicitly told.
To understand the above statement clearly let’s see one more example :
def foo(): x = 2 print(x) x = 5 print(x) foo() print(x)
The output of the above code is:
5
2
5
If you are familiar with C or C++ you may expect:
5
2
2
As your output but in Python the x declared inside the function is treated as local variable and has no relation with the x declared outside the function.
To use the x as global variable in foo we should use global keyword, x inside the function will then be treated as a global variable.
Here is an example:
def foo(): global x x = 2 print(x) x = 5 print(x) foo() print(x)
The output here is:
5
2
2
Using the global keyword we can make the function use the global copy of x and therefore any changes made to the function are reflected globally in x.
Why global keyword?
The global keyword is used because global variables are dangerous and are difficult to handle. Python wants to make sure that you really want to use global variable inside the function and therefore the global keyword is being used.
Let’s take an example to cover all the above points.
def hoo(): print(x) def boo(): x = 6 print(x) def foo(): global x x = 7 print(x) x = 5 hoo() # print 5 boo() # print 6 hoo() # print 5 foo() # print 7 print(x) # print 7 hoo() # print 7 boo() # print 6
The output of the above program is:
5
6
5
7
7
7
6
Lets try to understand above code.
- First call to hoo prints the global variable as it is just reading the variable x.
- Calling boo will create a local copy of x inside the function and initialize it with 6 and therefore it will print 6. After the call to function is finished the local variable is discarded. This function will not affect global x.
- As x is unaffected calling hoo again will print the global value of x i.e. 5
- foo is using the global keyword and therefore it will now use the global copy of x and change made will directly change global x.
- After the call to foo is executed value of global x is changed to 7 so print(x) will print 7.
- hoo again will simply print the global value of x i.e. 7
- Call to boo will create a local variable x with value 6 and will print it and global x will remain unchanged.
I hope now you got the idea about python global variables. Comment down below if you still have any queries.
The post Python Global Variables appeared first on The Crazy Programmer.
No comments:
Post a Comment