Skip to content
Home » Scope and Lifetime of Variables

Scope and Lifetime of Variables


Scope and Lifetime of Variables in Python

In Python, scope and lifetime determine:

✔ Where a variable can be accessed
✔ How long a variable exists in memory

These concepts are very important in functions and control structures.


————————————–

1. Scope of a Variable

Scope = the region of the program where a variable is accessible (visible).

Python follows the LEGB Rule for scope:

L – Local

E – Enclosing (Nonlocal)

G – Global

B – Built-in


A. Local Scope (Inside Function)

A variable defined inside a function.

def func():
    x = 10   # local variable
    print(x)

func()
print(x)     # ❌ Error: x not accessible outside function

✔ Exists only inside function
✔ Not accessible outside


B. Global Scope (Outside Function)

A variable defined outside all functions.

x = 20   # global variable

def func():
    print(x)  # accessible inside function

func()

✔ Accessible throughout program
✔ Accessible inside functions (read only)


C. Enclosing Scope (Nonlocal)

Variables in inner function can access variables of outer function.

def outer():
    x = 10    # enclosing variable
    def inner():
        print(x)
    inner()

outer()

If you want to modify enclosing variable, use nonlocal.

def outer():
    x = 10
    def inner():
        nonlocal x
        x = 20
    inner()
    print(x)   # 20

D. Built-in Scope

Functions and variables built into Python:

Examples:

  • len()
  • sum()
  • range()
  • print()

Available everywhere.


————————————–

2. Lifetime of a Variable

Lifetime = duration for which a variable exists in memory.


A. Lifetime of Local Variables

Local variables:

✔ Created when function is called
✔ Destroyed when function finishes

def func():
    x = 10
    print(x)

func()  
print(x)   # ❌ not available

Lifetime ends after function execution.


B. Lifetime of Global Variables

Global variables:

✔ Created when program starts
✔ Destroyed when program ends

x = 50

def show():
    print(x)

show()

C. Lifetime of Enclosing Variables

Enclosing scope variables exist as long as the outer function exists.


————————————–

3. Using global Keyword

If you want to modify a global variable inside a function, use global.

x = 10

def update():
    global x
    x = 20

update()
print(x)   # 20

4. Using nonlocal Keyword

Used to modify an enclosing function variable inside inner function.

def outer():
    x = 5
    def inner():
        nonlocal x
        x = 15
    inner()
    print(x)

outer()

————————————–

5. LEGB Rule Summary

LevelDescription
L – LocalInside the current function
E – EnclosingInside outer functions
G – GlobalTop-level of module
B – Built-inPython’s built-in scope

Python searches variables in this order:
Local → Enclosing → Global → Built-in


————————————–

Exam-Ready Summary

Scope defines where a variable is accessible
Lifetime defines how long a variable exists in memory

Local Variable

  • Declared inside function
  • Exists till function ends

Global Variable

  • Declared outside functions
  • Exists till program ends

Enclosing Variable

  • Defined in outer function
  • Accessible to inner function

global keyword

Used to modify global variables

nonlocal keyword

Used to modify enclosing variables