[Python-Dev] Improve error message "UnboundLocalError: local variable referenced before assignment"

Chris Angelico rosuav at gmail.com
Thu Nov 1 02:45:44 CET 2012

On Thu, Nov 1, 2012 at 12:20 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> The current wording is an exact, concise, description of the problem. Rather
> than tinkering with the wording, I think a more general solution might be a
> new HOWTO: Understanding exception messages. It could have alphabetically
> sorted entries for exceptions and messages that people find problematic.
> UnboundLocalError
>   local variable referenced before assignment
> ...
> Remedy: If you intend 'x' to refer to a glocal or nonlocal name, add the
> necessary global or nonlocal declaration. If you intend

+1. Can this be tied in with help(UnboundLocalError) perhaps? At the
moment, that produces a huge screed of details that aren't
particularly helpful to a novice (all its methods etc), and only a
one-line explanation "Local name referenced but not bound to a
value.". If that could be shortened and expanded on, it'd be a logical
place to point people. "You got an error you don't understand? Go to
the interactive interpreter and type help(NameOfError) - that should
tell you what it is."


More information about the Python-Dev mailing list