local variable referenced before assignment

P

Pete Bartonly

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?

Thanks!
 
A

A.T.Hofkamp

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

This should work, are you sure you didn't make a typo in one of the names?

Another way to make this fail would be when the if-condition is outside
the loop (is the indentation correct in your code?).

A short demontration:.... for item in fnr:
.... w,m = item
.... if m == 2:
.... print w
....
1

With respect to compactness and style, you can move your multi-assignment
statement in the for loop, as in

for tightestOwner, logMsg in filesNeedingReview:

Also, brackets around conditions (in the if) are not needed, and comparing
against None is usually done with 'is' or 'is not' instead of '==' or '!='.
The result is then

if logMsg is not None:

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?

No, you'd get an error at that point in that case.


Sincerely,
Albert
 
P

Peter Otten

Pete said:
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
 
A

Arnaud Delobelle

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

Check your indentation?
Seems to me that you might really have:

def review(...):
for ...:
....
if (logMsg...):
....

HTH
 
T

Tim Williams

Also, brackets around conditions (in the if) are not needed, and comparing
against None is usually done with 'is' or 'is not' instead of '==' or '!='.
The result is then

if logMsg is not None:

Or just
do_something()

:)
 
P

Pete Bartonly

A.T.Hofkamp said:
This should work, are you sure you didn't make a typo in one of the names?

Nope, the above is verbatim. This is why I'm so confused. It should
work! I'm editing in emacs, and the indents are tab chars. I've
re-indented the indents using 'tab' key - same result.

The entire error output is this:

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

I'm rather stuck at what to try next!

thanks.
Pete






Another way to make this fail would be when the if-condition is outside
the loop (is the indentation correct in your code?).

A short demontration:... for item in fnr:
... w,m = item
... if m == 2:
... print w
...
fnr = [(1,2), (3,4)]
r(fnr)
1

With respect to compactness and style, you can move your multi-assignment
statement in the for loop, as in

for tightestOwner, logMsg in filesNeedingReview:

Also, brackets around conditions (in the if) are not needed, and comparing
against None is usually done with 'is' or 'is not' instead of '==' or '!='.
The result is then

if logMsg is not None:

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?

No, you'd get an error at that point in that case.


Sincerely,
Albert
 
P

Pete Bartonly

With respect to compactness and style, you can move your multi-assignment
statement in the for loop, as in
[snip]

Btw, thanks for the tips on style too!
Pete
 
P

Pete Bartonly

Peter said:
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
 
P

Pete Bartonly

Pete said:
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?

Thanks!

Argh! Mea culpa everyone!
Turns out that there is a similar few lines of code later on in the
code. I was confusing which was which. (I was using meta-x goto-line but
then looking at the wrong but somehow, as it appeared on the same screen
in emacs.)
So the error was occuring at a similar line in the code, but one at
whihc logMsg *wasn't* being set beforehand.

Sorry all, thanks for your help, appreciate it!
Pete
 
D

Dennis Lee Bieber

^^^^^^^^^^^^^^^^^^

Here it the code again, directly cut and pasted from emacs:
Better
for item in filesNeedingReview:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_paths:
if (not tightestOwner in emailListForReviewers):
emailListForReviewers.append(tightestOwner)
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
Obviously the first post was NOT cut&paste, since the function names
(and parameter signature) have changed...

Given that, I'd suggest 'tis time to strip down to the absolute
minimum code that still produces the error...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top