473,503 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UnboundLocalError 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
"UnboundLocalError" error.

Here's the interesting part of the code:

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

#CONSTANTS
......
....
#connection to DB
dbcursor_=db.cursor()

#GLOBAL VARS
entryList_={}
cmterID_=0 //VARIABLE DECLARATION
projID_=0
fileIDNumber_=0
Commiter_={}
Commit_={}
Project_={}
ProjectVersion_={}
def updateCommiterTable(Commiter):
query="INSERT INTO Commiter (pk_cmterID,cmterName) VALUES
("+str(Commiter[0])+",\""+str(Commiter[1])+"\");"
dbcursor_.execute(query)
def updateCommitTable(Commit):
query="INSERT INTO Commit
(pk_cmitID,cmitTime,fk_cmterID,cmitProperties,cmit Comment,cmitCommentLines,fk_projID)
VALUES (" \

+str(Commit[0])+",\""+str(Commit[1])+"\","+str(Commit[2])+",\""+str(Commit[3])+"\",\""+str(Commit[4])+"\","+str(Commit[5])+","+str(Commit[6])+");"
dbcursor_.execute(query)
def updateProjectTable(Project):
dbcursor_.execute("INSERT INTO Project
(pk_projID,projName,projWebsite,projContactPoint,p rojSrcPath,projMailPath)
VALUES (" \

+str(Project[0])+",\""+str(Project[1])+"\",\""+str(Project[2])+"\",\""+str(Project[3])+"\",\""+str(Project[4])+"\",\""+str(Project[5])+"\");")
def updateProjectVersionTable(ProjectVersion):
dbcursor_.execute("INSERT INTO ProjectVersion
(pfk_projID,projName,projVersion) VALUES (" \

+str(ProjectVersion[0])+",\""+str(ProjectVersion[1])+"\",\""+str(ProjectVersion[2])+"\");");

def getLogsLoop():

while
svnLogging_.getCurrentRevisionNumber()!=svnLogging _.getLatestRevisionNumber():
try:
entryList_=svnLogging_.getNextLogs(PIVOT);
except HeadRevisionReachedException:
print "Attempting to go over the HEAD revision..."

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

entryList_.clear()
def processLogEntry(entry):
revision = int(entry.getRevision())
commiter = str(entry.getAuthor())
datetime = getTimeStamp(entry.getDate())
message = str(entry.getMessage())

Commiter_[0] = cmterID_ //HERE's THE PROBLEM
Commiter_[1] = commiter
updateCommiterTable(Commiter_)
Commit_[0] = revision
Commit_[1] = datetime
Commit_[2] = cmterID_
Commit_[3] = "" #properties
Commit_[4] = message
Commit_[5] = getNumberOfLines(message)
Commit_[6] = projID_
updateCommitTable(Commit_)

ProjectVersion_[0]=projID_
ProjectVersion_[1]=""
ProjectVersion_[2]=""
updateProjectVersionTable(ProjectVersion_)

Project_[0]=projID_
Project_[1]=""
Project_[2]=""
Project_[3]=""
Project_[4]=""
Project_[5]=""
updateProjectTable(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
UnboundLocalError: local: 'cmterID_'
================================================== ========================================

The things is, that cmterID_ HAS BEEN instantiated... I don't understand.
Can somebody please explain?
Sep 9 '07 #1
2 2042
def processLogEntry(entry):
# ADD THIS TO YOUR CODE
global cmterID_
>
revision = int(entry.getRevision())
commiter = str(entry.getAuthor())
datetime = getTimeStamp(entry.getDate())
message = str(entry.getMessage())

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.getRevision())
commiter = str(entry.getAuthor())
datetime = getTimeStamp(entry.getDate())
message = str(entry.getMessage())

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
2584
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...
6
9209
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...
2
5012
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...
8
1600
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:...
15
2305
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...
9
2406
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
1091
by: Terry Reedy | last post by:
Mr SZ wrote: you need global ORIG_TYPE,ORIG_MSG because this otherwise marks them as local.
1
6539
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
7202
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,...
0
7462
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
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...
0
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
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...

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.