Flat_Eric Contemplating Data Type Conversions

Scope

Scope

As your programs grow larger and your functions become more complex a natural question starts to emerge. Where can I actually use this variable? The answer depends on something called scope.

Scope refers to the part of your program where a variable exists and can be accessed. Not every variable is available everywhere in your code. Where you define a variable determines where you can use it.

Python has four levels of scope and they are evaluated in a specific order known as the LEGB rule:

When Python encounters a variable name it searches through these four levels in order, starting with Local and working outward. The first match it finds is the one it uses.

Local Scope

A variable created inside a function is local to that function. It only exists while the function is running and cannot be accessed from outside it. Once the function finishes the variable is gone.

The variable message only exists inside greet(). Trying to print it outside the function throws a NameError because as far as the rest of the program is concerned that variable does not exist.

Global Scope

A variable created at the top level of your file, outside of any function, is global. It can be read from anywhere in the file including inside functions. However reading a global variable and modifying it are two different things.

The function can read language because it is global. But if you try to reassign it inside the function without telling Python explicitly, it will create a new local variable with the same name instead of modifying the global one. This is one of the most common sources of confusion around scope.

Both print statements print Python because the assignment inside the function created a new local variable called language that only exists inside the function. The global language was never touched.

The global Keyword

If you genuinely need to modify a global variable from inside a function you can use the global keyword to tell Python explicitly that you want to work with the global version rather than creating a local one.

Use the global keyword sparingly. Modifying global variables from inside functions makes your code harder to follow and debug because the value of the variable can change from anywhere in the program. In most cases it is better to pass values in as arguments and return results instead.

Enclosing Scope

Enclosing scope applies when you have a function defined inside another function. The inner function has access to variables defined in the outer function even though they are not global. This is the E in LEGB.

The inner function inner() can read name from the outer function even though it was not passed in as an argument. This is enclosing scope in action.

The nonlocal Keyword

Just as global lets you modify a global variable from inside a function, the nonlocal keyword lets you modify a variable from an enclosing function rather than creating a new local one.

Without nonlocal, the assignment inside inner() would create a new local variable called count and the outer count would remain unchanged. With nonlocal, Python knows to modify the variable in the enclosing scope.

Built in Scope

The outermost level of scope is built in scope. This contains all the names that Python provides automatically without you having to define or import anything. Functions like print(), len(), range(), type() and input() all live in built in scope. They are always available everywhere in your program.

This is also why you should never name your own variables or functions the same as a built in. If you create a variable called list or print you will shadow the built in and Python will use your version instead, which will almost certainly cause unexpected behaviour.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Local vs Global

Exercise 2: The global Keyword

Exercise 3: Enclosing Scope

Exercise 4: The nonlocal Keyword

Exercise 5: Spot the Scope Bug

  • score = 0
  • def add_points(points):
  •     score = score + points
  •     return score
  • print(add_points(10))

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course