Scope

noun

say it: “skohp” /skoʊp/

The part of a program where a variable can be used: a variable made inside a function exists only inside that function.

What is scope in Python?

Scope is where in the code a variable can be used. A variable made inside a function only exists inside that function.

def checkout():
    total = 40
    print(total)   # works: inside the function

checkout()
print(total)       # NameError: total only exists inside checkout()

In a sentence

  • “total was out of scope after checkout() ended, so printing it gave a NameError.”
  • “Each function's variables have their own scope, so two functions can both use a name like price.”

Level: Apprentice · More Python terms at this level

Updated

In other subjects