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

Sam Partington sam.partington at gmail.com
Wed Nov 7 15:15:34 CET 2012

On 7 November 2012 13:57, Ulrich Eckhardt
<ulrich.eckhardt at dominolaser.com> wrote:
> Am 31.10.2012 23:15, schrieb Steven D'Aprano:
> I'll take a shot, since I was also bitten by this when trying to learn
> Python. The important point is that some code earlier or later in that
> function does an assignment, so this location should be referenced in the
> error.


> 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"?

Why not make use of the well defined words already there in the error
message, but at the line number

   "UnboundLocalError: Local variable 'FONT_NAMES' referenced before
it has been assigned to on line 11."


More information about the Python-Dev mailing list