English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Before reading this article, please make sure you have masteredPython global, local, and nonlocal variablesSome basic knowledge.
In Python, the global keyword allows you to modify a variable outside the current scope. It is used to create global variables and modify them in the local context.
The basic rule of the global keyword in Python is:
When we create a variable inside the function, it is local by default.
When we define a variable outside the function, it is global by default. We do not need to use the global keyword.
We use the global keyword to read and write global variables within the function.
Using global keyword outside the function is invalid
Let's take an example.
c = 1 # global variable def add(): print(c) add()
When we run the above program, the output will be:
1
However, in some cases, we need to modify global variables from within a function.
c = 1 # global variable def add(): c = c + 2 # increment c by 2 print(c) add()
When we run the above program, the output shows an error:
UnboundLocalError: local variable 'c' referenced before assignment
This is because we can only access global variables and cannot modify them from within a function.
The solution is to use the global keyword.
c = 0 # global variable def add(): global c c = c + 2 # increment by 2 print("Inside add():", c) add() print("In main:", c)
When we run the above program, the output will be:
Inside add(): 2 In main: 2
In the above program, we define c as a global keyword inside the add() function.
Then, increase the variable c1, which is c = c + 2. After that, we call the add() function. Finally, we print the global variable c.
As we have seen, the global variables outside the function have also changed c = 2.
In Python, we create a module config.py to save global variables and share information between Python modules in the same program.
This is how we share global variables between Python modules.
Create a config.py file to store global variables
a = 0 b = "empty"
Create an update.py file to change the global variable
import config config.a = 10 config.b = "alphabet"
Create a main.py file to test the change of values
import config import update print(config.a) print(config.b)
When we run the main.py file, the output will be
10 alphabet
In the above text, we create three files: config.py, update.py, and main.py.
The module config.py storesaandb'sglobal variables. In the update.py file, we import the config.py module and modifyaandbvalue. Similarly, in the main.py file, we import both the config.py and update.py modules. Finally, we print and test the value of the global variables, whether they have changed or not.
This is the method of using global variables in nested functions.
def foo(): x = 20 def bar(): global x x = 25 print("Before calling bar: ", x) print("Immediate call to bar") bar() print("After calling bar: ", x) foo() print("x in the main body: ", x)
Output is:
Before calling bar: 20 Immediate call to bar After calling bar: 20 x in the main body: 25
In the above program, we declare a global variable in the nested function bar(). In the foo() function, x has no effect on the global keyword.
The variable x accepts the value of the local variable before and after calling bar, that is, x =20. Outside the foo function, the variable x will take the value defined in the bar function, that is, x =25. This is because we used the global keyword to create a global variable in the bar function (local scope).
If we make any changes in the bar() function, these changes will appear outside the local scope, that is, foo().