Skip to main content

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:

Prompt
Create a function that defines a local variable x=300 and prints it. Call the function.
Output: complete Python program

Example

An inner function can read the local variable of its enclosing function:

Prompt
Create a function with a local variable x=300 and a nested inner function that prints x. Call the inner function from within the outer function.
Output: complete Python program

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:

Prompt
Create a global variable x=300. Print it from inside a function and outside.
Output: complete Python program

Example

First the function prints its local x, and then the main code prints the global one:

Prompt
Create a global variable x=300 and a function that sets a local x=200. Print x inside the function and outside to show they are separate.
Output: complete Python program

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:

Prompt
Use the global keyword inside a function to create a global variable x=300. Print x outside the function.
Output: complete Python program

Example

Declaring the variable with global lets the function change its value:

Prompt
Create a global variable x=300. Use the global keyword inside a function to change it to 200. Print x after calling the function.
Output: complete Python program

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:

Prompt
Use the 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

The LEGB Rule

To look up a variable name, Python applies the LEGB rule and tries these places in order:

  1. Local - The function running right now
  2. Enclosing - Any functions around it, moving from inner to outer
  3. Global - The top level of the module
  4. Built-in - The namespace of names built into Python

Example

The LEGB rule in action:

Prompt
Demonstrate the LEGB rule with nested functions. Set x to "global", "enclosing", and "local" at each level and print them.
Output: complete Python program