473,554 Members | 3,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

local variable referenced before assignment


Quick question, probably quite a simple matter. Take the follow start of
a method:
def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
This generates the error:

UnboundLocalErr or: 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!
Oct 25 '07 #1
9 17191
On 2007-10-25, Pete Bartonly <no**@try.inval idwrote:
>
Quick question, probably quite a simple matter. Take the follow start of
a method:
def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
This generates the error:

UnboundLocalErr or: 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 filesNeedingRev iew:

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
Oct 25 '07 #2
Pete Bartonly wrote:
Quick question, probably quite a simple matter. Take the follow start of
a method:
def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
This generates the error:

UnboundLocalErr or: 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
Oct 25 '07 #3
On Oct 25, 10:02 am, Pete Bartonly <n...@try.inval idwrote:
Quick question, probably quite a simple matter. Take the follow start of
a method:

def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:

This generates the error:

UnboundLocalErr or: local variable 'logMsg' referenced before assignment
Check your indentation?
Seems to me that you might really have:

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

HTH

--
Arnaud
Oct 25 '07 #4
On 25/10/2007, A.T.Hofkamp <ha*@se-162.se.wtb.tue. nlwrote:
On 2007-10-25, Pete Bartonly <no**@try.inval idwrote:
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()

:)
Oct 25 '07 #5
On 2007-10-25, Tim Williams <td*******@gmai l.comwrote:
On 25/10/2007, A.T.Hofkamp <ha*@se-162.se.wtb.tue. nlwrote:
>On 2007-10-25, Pete Bartonly <no**@try.inval idwrote:
>
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
Oct 25 '07 #6
A.T.Hofkamp wrote:
On 2007-10-25, Pete Bartonly <no**@try.inval idwrote:
>Quick question, probably quite a simple matter. Take the follow start of
a method:
def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
This generates the error:

UnboundLocalErr or: 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(te mpDir)
File "checkCode. py", line 448, in analyseFiles
analyseFilesInA Repos(startDir, f)
File "checkCode. py", line 590, in analyseFilesInA Repos
makeReport(proj Name, filesNeedingRev iew, filesFailedRevi ew)
File "checkCode. py", line 422, in makeReport
for logInfo in logMsg.changed_ paths:
UnboundLocalErr or: 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 filesNeedingRev iew:

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
Oct 25 '07 #7
A.T.Hofkamp wrote:
On 2007-10-25, Pete Bartonly <no**@try.inval idwrote:
>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
Oct 25 '07 #8
Peter Otten wrote:
Pete Bartonly wrote:
>Quick question, probably quite a simple matter. Take the follow start of
a method:
def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
This generates the error:

UnboundLocalErr or: 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 filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
if (not tightestOwner in emailListForRev iewers):
emailListForRev iewers.append(t ightestOwner)


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(te mpDir)
File "checkCode. py", line 445, in analyseFiles
analyseFilesInA Repos(startDir, f)
File "checkCode. py", line 587, in analyseFilesInA Repos
makeReport(proj Name, filesNeedingRev iew, filesFailedRevi ew)
File "checkCode. py", line 419, in makeReport
for logInfo in logMsg.changed_ paths:
UnboundLocalErr or: local variable 'logMsg' referenced before assignment
Pete
Oct 25 '07 #9
Pete Bartonly wrote:
>
Quick question, probably quite a simple matter. Take the follow start of
a method:
def review(filesNee dingReview):

for item in filesNeedingRev iew:
(tightestOwner, logMsg) = item

if (logMsg != None):
for logInfo in logMsg.changed_ paths:
This generates the error:

UnboundLocalErr or: 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

Oct 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

166
8533
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
2
5019
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 init to 0, like this colorIndex = 0 and in one of my functions, I increment it by 1
3
2977
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():
9
1687
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 format_t(): row= for col in table_to_process:
8
3870
by: Sullivan WxPyQtKinter | last post by:
I am confused by the following program: def f(): print x x=12345 f() result is: 12345
2
1731
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 388, in <module> ParseAll() File "C:\UDR2\UDRxmlGateway.py", line 371, in ParseAll if (PfFlag == 1):
2
1517
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 this case, Python doesn't find a "value" variable in this scope, so it checks the outer scope, and does find it. But then when it gets to the "a = "...
1
6549
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
0
7581
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5423
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2006
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1114
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
823
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.