local variable referenced before assignment

Pete Bartonly none at try.invalid
Thu Oct 25 11:58:58 EDT 2007


Peter Otten wrote:
> 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. 

I am! See my other reply just now.

Here it the code again, directly cut and pasted from emacs:

     for item in filesNeedingReview:
         (tightestOwner, logMsg) = item

         if (logMsg != None):
             for logInfo in logMsg.changed_paths:
                 if (not tightestOwner in emailListForReviewers):
                     emailListForReviewers.append(tightestOwner)




This triggers the error
> when review() is called with an empty sequence.
> 
> Please remember to copy and paste both code and traceback next time.

Sorry 'bout that. The traceback I forgot is:

Traceback (most recent call last):
   File "checkCode.py", line 599, in ?
     analyseFiles(tempDir)
   File "checkCode.py", line 445, in analyseFiles
     analyseFilesInARepos(startDir, f)
   File "checkCode.py", line 587, in analyseFilesInARepos
     makeReport(projName, filesNeedingReview, filesFailedReview)
   File "checkCode.py", line 419, in makeReport
     for logInfo in logMsg.changed_paths:
UnboundLocalError: local variable 'logMsg' referenced before assignment


Pete



More information about the Python-list mailing list