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

Terry Reedy tjreedy at udel.edu
Thu Nov 1 02:20:01 CET 2012


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.

UnboundLocalError
   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



-- 
Terry Jan Reedy



More information about the Python-Dev mailing list