Variable scope

The scope of a variable is the part of the program where it is accessible. A variable created in a function is local and disappears at the end of the call; a variable defined outside a function is global. A local variable with the same name shadows the global one without modifying it.

Example

x = 10
def f():
    x = 5  # local variable
f()
print(x)  # 10

Related concepts

The whole glossary