Scope
Where you create a variable decides where you can use it: only inside that region. This idea is called scope.
Local Scope
When you create a variable inside a function, it lives in that function's local scope and can't be used anywhere else.
So x in the examples below can't be reached from outside the function, though any function defined within that function can still use it.
Example
Inside the function that created it, the variable works as usual:
Output: complete Python program
def myfunc():
x = 300
print(x)
myfunc()
Example
An inner function can read the local variable of its enclosing function:
Output: complete Python program
def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()
Global Scope
Create a variable in the main body of your Python code and it becomes a global variable, part of the global scope.
You can reach a global variable from every scope, both global and local.
Using one name both inside and outside a function gives you two separate variables. The one outside the function lives in the global scope, and the one inside it lives in the local scope.
Example
Created outside any function, this variable is global, so all code can use it:
Output: complete Python program
x = 300
def myfunc():
print(x)
myfunc()
print(x)
Example
First the function prints its local x, and then the main code prints the global one:
Output: complete Python program
x = 300
def myfunc():
x = 200
print(x)
myfunc()
print(x)
Global Keyword
Stuck in a local scope but need to create a global variable? The global keyword does exactly that.
It turns the variable into a global one.
You also need this keyword when a function has to change the value of an existing global variable.
Example
Thanks to global, x is part of the global scope:
global keyword inside a function to create a global variable x=300.
Print x outside the function.
Output: complete Python program
def myfunc():
global x
x = 300
myfunc()
print(x)
Example
Declaring the variable with global lets the function change its value:
global keyword inside a function to change it to 200.
Print x after calling the function.
Output: complete Python program
x = 300
def myfunc():
global x
x = 200
myfunc()
print(x)
Nonlocal Keyword
Nested functions use the nonlocal keyword to work with the variables of the function around them.
With it, the variable belongs to the outer function.
Example
Because of nonlocal, x here is the outer function's variable:
nonlocal keyword in a nested function to modify the outer function's variable.
Set x="Jane" in outer, change to "hello" in inner, return x.
Output: complete Python program
def myfunc1():
x = "Jane"
def myfunc2():
nonlocal x
x = "hello"
myfunc2()
return x
print(myfunc1())
The LEGB Rule
To look up a variable name, Python applies the LEGB rule and tries these places in order:
- Local - The function running right now
- Enclosing - Any functions around it, moving from inner to outer
- Global - The top level of the module
- Built-in - The namespace of names built into Python
Example
The LEGB rule in action:
Output: complete Python program
x = "global"
def outer():
x = "enclosing"
def inner():
x = "local"
print("Inner:", x)
inner()
print("Outer:", x)
outer()
print("Global:", x)