Skip to content
AgileLogics
4 min read

Scope

Prerequisites: Functions

Scope is where a variable can be seen and used. A variable created inside a function is local — it only exists while that function is running, and is invisible outside it.

def set_total():
    total = 100
    print(total)   # works: total is visible here

set_total()
print(total)       # NameError: total does not exist out here

Common mistakes

  • Expecting a variable created inside a function to be usable outside it. If you need the value outside, return it and store the result in a variable at the outer level.