Scope

Scope defines where a variable can be seen and used in a program. A variable created inside a function (local scope) usually cannot be accessed from outside it.

General Programming

Global scope is accessible everywhere; local scope is limited to a function or block. Keeping variables in the smallest scope possible prevents accidental name clashes and bugs.

Examples

Python

def f():
    x = 10  # local to f
    print(x)

f()
print(x)  # error: x is not defined here

Output:

10
NameError: name 'x' is not defined

See also

Back to the Dictionary