Prerequisites: Global and Local Variables in Python
Before moving to nonlocal and global in Python. Let us consider some basic scenarios in nested functions.
The variable var1 has scope across entire fun(). It will be accessible from nested function of fun()
10 10853920 20 10854240
In this case gun() initialized new variable var1 in the scope of gun. var1 with value 10 and var1 with value 20 are two different and unique variables. var1 holding value 20 will be by default accessed in
Considering the previous example, we know that guns will initialize a new variable var1 in its own scope. But when it is going to do so, it cannot find the value of var1 yet,
to perform arithmetic operation as no value has been assigned to var1 previously in gun().
In this example, before initializing var1 in gun(). We have explicitly told Python that, do not initialize a new variable, instead access var1 present already on line 2. using the keyword nonlocal So when interpreter performs addition, it access the value 10(already present) and error is avoided.
Let us move on to global keyword now. Consider the examples given below
var1 is 10
Global variable var1 will be simply read or accessed inside function fun()
var1 is 20 var1 is at 10854240 var1 is 10 var1 is at 10853920
A new variable var1 will be initialized in fun(). Global variable var1 will be different from local variable var1 of fun(). By default in fun(), the local variables will be accessed.
Considering previous example, we know that fun() will initialize a new variable var1 in its own scope. But when it is going to do so, it cannot find the value of var1 yet,
to perform arithmetic operation as no value has been assigned to var1 previously in fun()
var1 is 30
In this example, before initializing var1 in fun(). We have explicitly told python that, do not initialize a new variable, instead access var1 present already on line 1. using the keyword global So when interpreter performs addition, it access the value 10(already present) and error is avoided.
- Global keyword in Python
- Global and Local Variables in Python
- What is the Python Global Interpreter Lock (GIL)
- Python in Keyword
- is keyword in Python
- Keyword Module in Python
- finally keyword in Python
- Python | assert keyword
- Python | yield Keyword
- Python IMDbPY - Searching keyword
- Python | Passing dictionary as keyword arguments
- Python program to check if a given string is Keyword or not
- How to check if a string is a valid keyword in Python?
- Python IMDbPY – Searching movies matching with keyword
- PyQt5 QCalendarWidget - Mapping co-ordinate system to global
- PyQt5 QCalendarWidget - Mapping co-ordinate system from global
- Python - Read blob object in python using wand library
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- Python | Convert list to Python array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.