Skip to content

Scope Lifetime

Local Scope

1. Function Call

def function():
    x = 10  # Created at call
    return x
    # Destroyed after return

result = function()
# x no longer exists

2. Frame Lifetime

def outer():
    x = 10

    def inner():
        return x

    return inner
    # Frame ends but x kept for closure

f = outer()
print(f())  # x still accessible

Global Scope

1. Module Lifetime

# Exists for program lifetime
x = 10

def function():
    print(x)

# x available throughout

Summary

  • Local: function call duration
  • Enclosing: kept if captured
  • Global: program lifetime
  • Built-in: always available