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

Rob Cliffe rob.cliffe at btinternet.com
Thu Nov 1 09:36:51 CET 2012

On 01/11/2012 01:45, Chris Angelico wrote:
> 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."
> ChrisA
An excellent idea IMO.  +1
Rob Cliffe
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/rob.cliffe%40btinternet.com

More information about the Python-Dev mailing list