What is UnboundLocalError for?

Paul Prescod paul at prescod.net
Wed Jul 12 18:55:56 EDT 2000


Alex wrote:
> 
> (Python 2.0 question.)
> 
> I was wondering what situations the UnboundLocalError exception is
> intended to be thrown in.  I encountered it when I made a mistake like
> this:
> 
> >>> f()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 1, in f
> UnboundLocalError: l

Well you didn't show the code that gave you the error, but it probably
would have given you a pretty good idea. An UnboundLocal is called when
you assign to a local in a function but refer to it first. It's like
referring to something before you declare it in languages that have
variable declarations. In Python, the assignment *is* the declaration.
-- 
 Paul Prescod - Not encumbered by corporate consensus
Simplicity does not precede complexity, but follows it. 
	- http://www.cs.yale.edu/~perlis-alan/quotes.html




More information about the Python-list mailing list