Global keyword is a keyword that allows a user to modify a variable outside of the current scope. It is used to create global variables from a non-global scope i.e inside a function. Global keyword is used inside a function only when we want to do assignments or when we want to change a variable. Global is not needed for printing and accessing.
Rules of global keyword:
- If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.
- Variables that are only referenced inside a function are implicitly global.
- We Use global keyword to use a global variable inside a function.
- There is no need to use global keyword outside a function.
Use of global keyword:
To access a global variable inside a function there is no need to use global keyword.
If we need to assign a new value to a global variable then we can do that by declaring the variable as global.
Code 2: Without global keyword
UnboundLocalError: local variable 'a' referenced before assignment
This output is an error because we are trying to assign a value to a variable in an outer scope. This can be done with the use of global variable.
Code 3 : With global keyword
Value of x inside a function : 20 Value of x outside a function : 20
In the above example, we first define x as global keyword inside the function
change(). The value of x is then incremented by 5, ie. x=x+5 and hence we get the output as 20.
As we can see by changing the value inside the function
change(), the change is also reflected in the value outside the global variable.
Global variables across python modules :
The best way to share global variables across different modules within the same program is to create a special module (often named config or cfg). Import the config module in all modules of your application; the module then becomes available as a global name. There is only one instance of each module and so any changes made to the module object get reflected everywhere. For Example, sharing global variables across modules
Code 1: Create a
config.py file to store global variables:
Code 2: Create a
modify.py file to modify global variables:
Here we have modified the value of x, y, and z. These variables were defined in the module
config.py, hence we have to import
config module and we can use
config.variable_name to access these variables.
Code 3: Create a
main.py file to modify global variables:
1 2 geeksforgeeks
Global in Nested functions
In order to use global inside a nested functions, we have to declare a variable with global keyword inside a nested function
Before making changing: 15 Making change After making change: 15 value of x 20
In the above example Before and after making change(), the variable
x takes the value of local variable i.e x = 15. Outside of the
add() function, the variable
x will take value defined in the
change() function i.e x = 20. because we have used global keyword in
x to create global variable inside the change() function (local scope).
- Global and Local Variables in Python
- What is the Python Global Interpreter Lock (GIL)
- finally keyword in Python
- Python | yield Keyword
- Python | assert keyword
- 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 | Passing dictionary as keyword arguments
- What are the AngularJs Global API?
- How to declare a global variable in PHP?
- Throw Keyword in Scala
- How to implement is functionality without using is keyword in C#
- Invoking an overloaded constructor using this keyword in C#
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Python | Merge Python key values to list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.