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! 9 17159
On 2007-10-25, Pete Bartonly <no**@try.invalidwrote:
>
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:
>>def r(fnr):
.... 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
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
On Oct 25, 10:02 am, Pete Bartonly <n...@try.invalidwrote:
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
--
Arnaud
On 25/10/2007, A.T.Hofkamp <ha*@se-162.se.wtb.tue.nlwrote:
On 2007-10-25, Pete Bartonly <no**@try.invalidwrote:
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
>> if logMsg:
do_something()
:)
On 2007-10-25, Tim Williams <td*******@gmail.comwrote:
On 25/10/2007, A.T.Hofkamp <ha*@se-162.se.wtb.tue.nlwrote:
>On 2007-10-25, Pete Bartonly <no**@try.invalidwrote:
>
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
>>> if logMsg:
do_something()
:)
That is not the same.
If logMsg is 0, False, or empty string, the second variant
would be False and not True.
Albert
A.T.Hofkamp wrote:
On 2007-10-25, Pete Bartonly <no**@try.invalidwrote:
>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?
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:
>>>def r(fnr):
... 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
A.T.Hofkamp wrote:
On 2007-10-25, Pete Bartonly <no**@try.invalidwrote:
>Quick question, probably quite a simple matter. Take the follow start of a method:
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
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
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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Graham |
last post by:
This has to do with class variables and instances variables.
Given the following:
<code>
class _class:
var = 0
#rest of the class
|
by: silverburgh.meryl |
last post by:
Can you please tell me what is the meaning this error in general?
UnboundLocalError: local variable 'colorIndex' referenced before
assignment
In my python script,
I have a variable define and...
|
by: Hari Sekhon |
last post by:
I've got some code as follows:
import re
re_regexname = re.compile('abc')
......
...... various function defs
......
def func1():
|
by: Pyenos |
last post by:
#############################CODE##############################
t_len=0
class WORK:
def getwork(self):
def formattable(table_to_process,type):
TYPE= #list of types to format
if type==TYPE:
def...
|
by: Sullivan WxPyQtKinter |
last post by:
I am confused by the following program:
def f():
print x
x=12345
f()
result is:
12345
|
by: Wang, Harry |
last post by:
$$ TestCase ID : 001
Step : deleteDvc,206268
Result Eval type : XmlChk
Step : deleteDvc,206269
Result Eval type : XmlChk
Traceback (most recent call last):
File "C:\UDR2\UDRxmlGateway.py", line...
|
by: Matt Nordhoff |
last post by:
Sebastjan Trepca wrote:
Python doesn't like when you read a variable that exists in an outer
scope, then try to assign to it in this scope.
(When you do "a = b", "b" is processed first. In...
|
by: nongnaeja |
last post by:
I don't know ,why it's error
This my code
from PIL import Image
def ch_red(tmp):
R = tmp
G = tmp
B = tmp
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |