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

Nick Coghlan ncoghlan at gmail.com
Wed Nov 7 16:01:59 CET 2012


On Wed, Nov 7, 2012 at 11:57 PM, Ulrich Eckhardt
<ulrich.eckhardt at dominolaser.com> wrote:
> How about:
>
>   "UnboundLocalError: Local variable 'FONT_NAMES' (created on
>    line 11) referenced before assignment."
>
> What I don't really like is the term "created". Maybe "implicitly created on
> line 11"? Or "implied by line 11"? Or how about "Local variable FONT_NAMES
> (implied by line 11) doesn't refer to an object", to avoid the multiple
> interpretations of the term "assignment"?

Unfortunately, we don't track the information we would need in order
to emit that kind of error message. However, you did give me an idea:
I believe the compiler is actually in a position to emit SyntaxWarning
for functions that have a high chance of triggering UnboundLocalError
when called. With output pointing to *both* problematic lines,
beginners should stand a better chance of figuring out what is going
on. (http://bugs.python.org/issue16429)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list