n00b confusion re: local variable referenced before assignment error

Falcolas garrickp at gmail.com
Fri Jun 19 18:30:09 CEST 2009


On Jun 19, 10:16 am, Wells Oliver <we... at submute.net> wrote:
> Writing a class which essentially spiders a site and saves the files
> locally. On a URLError exception, it sleeps for a second and tries again (on
> 404 it just moves on). The relevant bit of code, including the offending
> method:
>
> [snip]
>
> But what I am seeing is that after a retry (on catching a URLError
> exception), I see bunches of "UnboundLocalError: local variable 'handler'
> referenced before assignment" errors on line 38, which is the
> "file.write(handler.read())" line..
>
> What gives?

'Handler' is only assigned in the try statement, so if you error into
your exception clause, nothing will have been bound to the name
'handler', causing the exception you're seeing.



More information about the Python-list mailing list