Python Scope of Variables
In Python, variables are the containers for storing data values. They are reference, or pointers, to an object in memory which means that whenever a variable is assigned to an instance, it gets mapped to that instance. Unlike other languages like
Python is not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.
45 1456.8 John
Note: To know more about variables click here.
Scope of variable
The location where we can find a variable and also access it if required is called the scope of a variable.
Global and local variables
Global variables are the ones that are defined and declared outside any function and are not specified to any function. They can be used by any part of the program.
I love Geeksforgeeks
Now suppose a variable with the same name is defined inside the scope of function as well then it will print the value given inside the function only and not the global value.
Me too. I love Geeksforgeeks
The variable s is defined as the string “I love Geeksforgeeks”, before we call the function
f(). The only statement in
f() is the
print(s) statement. As there is no local s, the value from the global s will be used.
The question is, what will happen if we change the value of
s inside of the function
f()? Will it affect the
global s as well? We test it in the following piece of code:
Traceback (most recent call last): File "/home/370cac45bae7f1e6096520b7a0edb604.py", line 13, in
f() File "/home/370cac45bae7f1e6096520b7a0edb604.py", line 3, in f print(s) UnboundLocalError: local variable 's' referenced before assignment
To make the above program work, we need to use
global keyword. We only need to use
global keyword in a function if we want to do assignments / change them.
global is not needed for printing and accessing. Why? Python “assumes” that we want a local variable due to the assignment to s inside of
f(), so the first print statement throws this error message. Any variable which is changed or created inside of a function is local, if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword
global, as can be seen in the following example:
Python is great! Look for Geeksforgeeks Python Section Look for Geeksforgeeks Python Section
Consider the below example for better understanding of the topic.
global : 1 Inside f() : 1 global : 1 Inside g() : 2 global : 1 Inside h() : 3 global : 3
nonlocal keyword is used in the case of nested functions. This keyword works similar to the global, but rather than global, this keyword declares a variable to point to the variable of outside enclosing function, in case of nested functions.
Value of a using nonlocal is : 10 Value of a without using nonlocal is : 5
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course