What is the global keyword
In Python, global
keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.
Rules of global Keyword
The basic rules for global
keyword in Python are:
- When we create a variable inside a function, it is local by default.
- When we define a variable outside of a function, it is global by default. You don’t have to use
global
keyword.
- We use
global
keyword to read and write a global variable inside a function.
- Use of
global
keyword outside a function has no effect.
-
Use Global
x = "global "
def foo():
global x
y = "local"
x = x * 2
print(x)
print(y)
foo()
output
global global
local
Example 1: Accessing global Variable From Inside a Function
c = 1 # global variable
def add():
print(c)
add()
output
1
This seems fine, however we may have some scenarios where we need to modify the global variable from inside a function.
Example 2: Modifying Global Variable From Inside the Function
c = 1 # global variable
def add():
c = c + 2 # increment c by 2
print(c)
add()
output error
UnboundLocalError: local variable ‘c’ referenced before assignment
This is because we can only access the global variable but cannot modify it from inside the function.
solution: use the ‘global’ keyword.
Example 3: Changing Global Variable From Inside a Function using global
c = 0 # global variable
def add():
global c
c = c + 2 # increment by 2
print("Inside add():", c)
add()
print("In main:", c)
output:
Inside add(): 2
In main: 2
Global in Nested Functions
Example 5: Using a Global Variable in Nested Function
def foo():
x = 20
def bar():
global x
x = 25
print("Before calling bar: ", x)
print("Calling bar now")
bar()
print("After calling bar: ", x)
foo()
print("x in main: ", x)
output
Before calling bar: 20
Calling bar now
After calling bar: 20
x in main: 25
In the above program, we declared a global variable inside the nested function bar()
. Inside foo()
function, x
has no effect of the global keyword.
Before and after calling bar()
, the variable x
takes the value of local variable i.e x = 20
. Outside of the foo()
function, the variable x
will take value defined in the bar()
function i.e x = 25
. This is because we have used global
keyword in x
to create global variable inside the bar()
function (local scope).
If we make any changes inside the bar()
function, the changes appear outside the local scope, i.e. foo()
.
Example of Scope and Namespace in Python
def outer_function():
b = 20
def inner_func():
c = 30
a = 10
Here, the variable a
is in the global namespace. Variable b
is in the local namespace of outer_function()
and c
is in the nested local namespace of inner_function()
.
When we are in inner_function()
, c
is local to us, b
is nonlocal and a
is global. We can read as well as assign new values to c
but can only read b
and a
from inner_function()
.
If we try to assign as a value to b
, a new variable b
is created in the local namespace which is different than the nonlocal b
. The same thing happens when we assign a value to a
.
However, if we declare a
as global, all the reference and assignment go to the global a
. Similarly, if we want to rebind the variable b
, it must be declared as nonlocal. The following example will further clarify this.
def outer_function():
a = 20
def inner_function():
a = 30
print('a =', a)
inner_function()
print('a =', a)
a = 10
outer_function()
print('a =', a)
output
a = 30
a = 20
a = 10
In this program, three different variables a
are defined in separate namespaces and accessed accordingly. While in the following program,
def outer_function():
global a
a = 20
def inner_function():
global a
a = 30
print('a =', a)
inner_function()
print('a =', a)
a = 10
outer_function()
print('a =', a)
output
a = 30
a = 30
a = 30
Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope.
Let’s see an example of how a nonlocal variable is used in Python.
We use nonlocal
keywords to create nonlocal variables.
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
output
inner: nonlocal
outer: nonlocal
In the above code, there is a nested inner()
function. We use nonlocal
keywords to create a nonlocal variable. The inner()
function is defined in the scope of another function outer()
.
Note : If we change the value of a nonlocal variable, the changes appear in the local variable.
Reference
- Programiz - Python Global, Local and Nonlocal variables
- Programiz - Python Global Keyword
- Python Namespace and Scope