What's wrong with this code? (UnboundLocalError: local variable referenced before assignment)

John Gordon gordon at panix.com
Mon Jun 24 17:41:54 EDT 2013


In <mailman.3767.1372106609.3114.python-list at python.org> Dave Angel <davea at davea.name> writes:

> > The problem is that change() isn't being executed here; instead it's being
> > executed from within root.mainloop(), whenever the user presses button-1.
> >
> > And within root.mainloop(), there is no variable called isWhite.
> >

> Actually that's irrelevant.  Whether or not there's one global with the 
> same name, or twenty-three object attributes with the same name, the 
> fact that there's a binding of the local makes that name a local.  The 
> only way to avoid that is not to bind, or to use global or nonlocal 
> declarations.

Quite right.  I should have verified my answer before posting.  Thanks
for setting me straight.  :-)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list