[Python-Dev] Improve error message "UnboundLocalError: local variable referenced before assignment"
tjreedy at udel.edu
Thu Nov 1 06:49:51 CET 2012
On 10/31/2012 9:20 PM, Terry Reedy wrote:
> On 10/31/2012 4:28 PM, R. David Murray wrote:
>> "local variable referenced before assignment" *is*
>> a pointer to the concept of when global variables become local...perhaps
>> there is a better wording, do you have a suggestion?
> 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.
> local variable referenced before assignment
> Problem: You used a local name 'x' in an expression before you assigned
> it a value. So the interpreter could not compute the value of the
> expression. Remember that assignment makes a name local unless it is
> declared global or nonlocal.
> Remedy: If you intend 'x' to refer to a glocal or nonlocal name, add the
> necessary global or nonlocal declaration. If you intend
<left out> 'x' to be local, rearrange your code to give it a value
before you use it.
Terry Jan Reedy
More information about the Python-Dev