local variable referenced before assignment

Peter Otten __peter__ at web.de
Thu Oct 25 05:18:17 EDT 2007


Pete Bartonly wrote:

> Quick question, probably quite a simple matter. Take the follow start of 
> a method:
> 
> 
> def review(filesNeedingReview):
> 
>      for item in filesNeedingReview:
>          (tightestOwner, logMsg) = item
> 
>          if (logMsg != None):
>              for logInfo in logMsg.changed_paths:
> 
> 
> This generates the error:
> 
>    UnboundLocalError: local variable 'logMsg' referenced before assignment
> 
> I thought I'd assigned it in the "(tightestOwner, logMsg) = item" line - 
> so in the python interpreter complaining about the fact this assignment 
> might not go well?

My crystal ball tells me that you are not posting the actual code where
for... and if... are indented to the same level. This triggers the error
when review() is called with an empty sequence.

Please remember to copy and paste both code and traceback next time.

Peter



More information about the Python-list mailing list