UnboundLocalError on global variable

GuillaumeC fructidor00 at gmail.com
Sun Sep 9 18:03:15 EDT 2007


> 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/07/python-functions-assignments-and-scope.html

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




More information about the Python-list mailing list