Built-in Exceptions

UnboundLocalError — exception class

A function reads a local variable before it has a value. Assignment anywhere in the function normally makes a name local unless declared global or nonlocal. Initialize the variable on every required branch or explicitly choose another scope. Catch UnboundLocalError when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
UnboundLocalError

Return Value

type

The class object UnboundLocalError; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
value=7def read():    result=value    value=8    return resulttry:    read()except UnboundLocalError as error:    print(type(error).__name__)
Python Reference