Nearby lessons
119 of 159Python - Global Keyword
- Understand the global keyword
- Declare a global variable inside a function
- Modify a global variable from inside a function
- See what happens when global is not used
- Access a global variable with globals()
What is the global Keyword?
The global keyword is used inside a function to work with global variables.
By default, when we assign a value to a name inside a function, Python creates a local variable. The global keyword changes this behaviour.
Two Purposes of the global Keyword
We can use the global keyword for the following 2 purposes:
- To declare a global variable inside a function.
- To make a global variable available to the function so that we can perform required modifications.
Example 1: Without the global Keyword
Consider what happens when we assign to a name inside a function without using global:
Explanation
Inside f1(), a = 777 creates a new local variable named a. It does not modify the global a.
So f1() prints the local value 777, while f2() still sees the original global value 10.
Example 2: Modifying a Global Variable
Now use the global keyword inside f1() so that the assignment changes the actual global variable:
Explanation
The global a statement inside f1() tells Python that a refers to the global variable, not a local one.
Therefore a = 777 modifies the global variable. Now f1() and f2() both print 777.
Comparison: Without vs With global
| Without global | With global |
|---|---|
| Creates a local variable. | Refers to the global variable. |
| Global value is unchanged. | Global value is modified. |
| Other functions still see the old value. | Other functions see the new value. |
Example 3: Accessing a Local Variable from Another Function
If a variable is declared only inside a function, it cannot be accessed from another function:
Explanation
The variable a is local to f1(). When f2() tries to access it, Python cannot find a in any scope and raises a NameError.
Example 4: Declaring a Global Variable Inside a Function
Using global, we can declare a brand new global variable from inside a function:
Explanation
There is no global a at module level. Inside f1(), the global a statement declares a as a global variable, and a = 10 gives it a value.
Now f2() can access the same global a and prints 10.
When Global and Local Variables Have the Same Name
If a global variable and a local variable have the same name, we can access the global variable inside the function using globals()['name']:
Explanation
Inside f1(), the plain name a refers to the local variable, so print(a) shows 777.
globals()['a'] returns the value of the global variable, which is 10.
Points to Remember
globalis used only inside a function.- It must be written before any use of the variable in that function.
- Without
global, an assignment inside a function always creates a local variable. globals()returns a dictionary of all global names and their values.- Use
globalonly when a function really needs to change a module-level variable.
- The global keyword is used inside a function to declare or modify a global variable
- Without global, assigning a value inside a function creates a local variable
- With global, the assignment modifies the module-level variable
- global also lets us declare a brand new global variable from inside a function
- globals()['name'] reads the global value even when a local variable has the same name