473,594 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UnboundLocalErr or on global variable

Hi,
i have a problem, the source of which is probably the fact, that i have
not understood how to declare global variables - I use the Jython
compiler, but i think this is a Python issue...

First of all, i don not use any classes in this module. The problem is,
that i declare and instantiate some vars outside the functions (global
ones), but when i use them inside the functions, i get an
"UnboundLocalEr ror" error.

Here's the interesting part of the code:

=============== =============== =============== =============== =============== ===============
#imports
....

#CONSTANTS
......
....
#connection to DB
dbcursor_=db.cu rsor()

#GLOBAL VARS
entryList_={}
cmterID_=0 //VARIABLE DECLARATION
projID_=0
fileIDNumber_=0
Commiter_={}
Commit_={}
Project_={}
ProjectVersion_ ={}
def updateCommiterT able(Commiter):
query="INSERT INTO Commiter (pk_cmterID,cmt erName) VALUES
("+str(Commi ter[0])+",\""+str(Com miter[1])+"\");"
dbcursor_.execu te(query)
def updateCommitTab le(Commit):
query="INSERT INTO Commit
(pk_cmitID,cmit Time,fk_cmterID ,cmitProperties ,cmitComment,cm itCommentLines, fk_projID)
VALUES (" \

+str(Commit[0])+",\""+str(Com mit[1])+"\","+str(Com mit[2])+",\""+str(Com mit[3])+"\",\""+str(C ommit[4])+"\","+str(Com mit[5])+","+str(Commi t[6])+");"
dbcursor_.execu te(query)
def updateProjectTa ble(Project):
dbcursor_.execu te("INSERT INTO Project
(pk_projID,proj Name,projWebsit e,projContactPo int,projSrcPath ,projMailPath)
VALUES (" \

+str(Project[0])+",\""+str(Pro ject[1])+"\",\""+str(P roject[2])+"\",\""+str(P roject[3])+"\",\""+str(P roject[4])+"\",\""+str(P roject[5])+"\");")
def updateProjectVe rsionTable(Proj ectVersion):
dbcursor_.execu te("INSERT INTO ProjectVersion
(pfk_projID,pro jName,projVersi on) VALUES (" \

+str(ProjectVer sion[0])+",\""+str(Pro jectVersion[1])+"\",\""+str(P rojectVersion[2])+"\");");

def getLogsLoop():

while
svnLogging_.get CurrentRevision Number()!=svnLo gging_.getLates tRevisionNumber ():
try:
entryList_=svnL ogging_.getNext Logs(PIVOT);
except HeadRevisionRea chedException:
print "Attempting to go over the HEAD revision..."

for entry in entryList_:
print "processing new SVN entry..."
processLogEntry (entry)

entryList_.clea r()
def processLogEntry (entry):
revision = int(entry.getRe vision())
commiter = str(entry.getAu thor())
datetime = getTimeStamp(en try.getDate())
message = str(entry.getMe ssage())

Commiter_[0] = cmterID_ //HERE's THE PROBLEM
Commiter_[1] = commiter
updateCommiterT able(Commiter_)
Commit_[0] = revision
Commit_[1] = datetime
Commit_[2] = cmterID_
Commit_[3] = "" #properties
Commit_[4] = message
Commit_[5] = getNumberOfLine s(message)
Commit_[6] = projID_
updateCommitTab le(Commit_)

ProjectVersion_[0]=projID_
ProjectVersion_[1]=""
ProjectVersion_[2]=""
updateProjectVe rsionTable(Proj ectVersion_)

Project_[0]=projID_
Project_[1]=""
Project_[2]=""
Project_[3]=""
Project_[4]=""
Project_[5]=""
updateProjectTa ble(Project_)

cmterID_+=1
projID_+1

############### ############### HELPER##METHODS ############### ############### #

....
############### ############### HELPER##METHODS ############### ############### #
getLogsLoop()
=============== =============== =============== =============== =============== ===============
And I get:
=============== =============== =============== =============== =============== ===============
Traceback (innermost last):
File "ParseSVN2DB.py ", line 182, in ?
File "ParseSVN2DB.py ", line 87, in getLogsLoop
File "ParseSVN2DB.py ", line 100, in processLogEntry
UnboundLocalErr or: local: 'cmterID_'
=============== =============== =============== =============== =============== ===============

The things is, that cmterID_ HAS BEEN instantiated... I don't understand.
Can somebody please explain?
Sep 9 '07 #1
2 2050
def processLogEntry (entry):
# ADD THIS TO YOUR CODE
global cmterID_
>
revision = int(entry.getRe vision())
commiter = str(entry.getAu thor())
datetime = getTimeStamp(en try.getDate())
message = str(entry.getMe ssage())

Commiter_[0] = cmterID_ //HERE's THE PROBLEM
The reason why it does not work is that there is an assignment to
cmterID_ in your function - even if it is after the problem. As a
consequence, the bytecode compiler makes this name local, and the name
is unbound. The quick fix is to add the global statement as above.

There is a good explanation here:
http://paddy3118.blogspot.com/2006/0...and-scope.html

May I suggest that you use less global variables, for instance
enclosing your method in a class?

Sep 9 '07 #2
def processLogEntry (entry):
# ADD THIS TO YOUR CODE
global cmterID_
>
revision = int(entry.getRe vision())
commiter = str(entry.getAu thor())
datetime = getTimeStamp(en try.getDate())
message = str(entry.getMe ssage())

Commiter_[0] = cmterID_ //HERE's THE PROBLEM
The reason why it does not work is that there is an assignment to
cmterID_ in your function - even if it is after the problem. As a
consequence, the bytecode compiler makes this name local, and the name
is unbound. The quick fix is to add the global statement as above.

There is a good explanation here:
http://paddy3118.blogspot.com/2006/0...and-scope.html

May I suggest that you use less global variables, for instance
enclosing your method in a class?

Sep 9 '07 #3

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

Similar topics

3
2591
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also happens with 2.2) An import within a function that shadows a global import can raise UnboundLocalError. I think the real problem is that the the local 'import sys' is seen as occuring after the use of sys within this function, even though sys is...
6
9216
by: Alex Gittens | last post by:
I'm trying to define a function that prints fields of given widths with specified alignments; to do so, I wrote some helper functions nested inside of the print function itself. I'm getting an UnboundLocalError, and after reading the Naming and binding section in the Python docs, I don't see why. Here's the error: >>> fieldprint(, 'rl', ) Traceback (most recent call last): File "<stdin>", line 1, in ?
2
5021
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
8
1604
by: David Bear | last post by:
I'm attempting to use the cgi module with code like this: import cgi fo = cgi.FieldStorage() # form field names are in the form if 'name:part' keys = fo.keys() for i in keys: try: item,value=i.split(':') except NameError, UnboundLocalError:
15
2317
by: Paddy | last post by:
Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing a list in similar circumstances. The documentation: http://docs.python.org/ref/naming.html does not give me enough info to determine why the difference exists as it does not seem to mention types at all..
9
2417
by: Camellia | last post by:
hi all why it generates an "UnboundLocalError" when I do the following: <code> .... def main(): number = number() number_user = user_guess() while number_user != number:
0
1111
by: Terry Reedy | last post by:
Mr SZ wrote: you need global ORIG_TYPE,ORIG_MSG because this otherwise marks them as local.
1
6550
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
7882
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8260
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5837
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5416
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3872
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2391
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
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.