UnboundLocalError: local variable referenced before assignment

Bryan bryanjugglercryptographer at yahoo.com
Tue Jun 8 09:30:06 EDT 2010


Peter Otten wrote:
> Chris Rebert wrote:
[...]
> > The only global variable defined in mkTable.py is the "mkTable"
> > function (partly since you don't import anything). So the reference to
> > the global variable "canvas" on the right-hand side of this expression
> > is completely undefined, resulting in the error you're getting.
>
> The above would produce a NameError, not an UnboundLocalError:

Ah, yes. The OP's code produces a NameError.

[...]
> To get the latter you'd have to omit the global declaration.

Not hard to diagnose: The OP got the UnboundLocalError, so he looked
stuff up and tried various things, such as the global declaration, but
still got an exception. In writing it up, he copied the initial
exception, but a latter version of the function.


--
--Bryan



More information about the Python-list mailing list