I've got a python script that sets a variable: notes_eo_counter = 0 And then defines a function: def printNotesRow(columns): if columns[3][:31] == 'Automatic person update notice': print "<tr" if (int(notes_eo_counter) % 2): print 'bgcolor="#ffffff">' else: print 'bgcolor="#' + str(session['CRBGColor']) + '">' print '<td class="bodysmall">' + str(context.fixDate(columns[0])) + '</td>' print '<td class="bodysmall">' + columns[1] + '</td>' print '<td class="bodysmall">' + str(columns[3][36:-1]) + '</td>' print '</tr>' notes_eo_counter = notes_eo_counter + 1 return printed And then calls it: newnotesrow = printNotesRow() if newnotesrow: print newnotesrow return printed This has worked just fine in several scripts, but in this particular one I'm gettin an error: Error Type: UnboundLocalError Error Value: local variable 'notes_eo_counter' referenced before assignment Those lines, although blended with many others, are in that order in the code. I don't know how I'm getting this error and it's driving me nuts. The line that the traceback points to is: if (int(notes_eo_counter) % 2): TIA Rick
D. Rick Anderson wrote:
I've got a python script that sets a variable:
notes_eo_counter = 0
And then defines a function:
def printNotesRow(columns):
Defining functions within functions (which is what you're effectively doing here) is pretty ropey. Best to seperate printNotesRow out into a seperate method. In the meantime, try declaring it like this: def printNotesRow(columns): global notes_eo_counter cheers, Chris
D. Rick Anderson wrote at 2003-3-15 23:21 -0800:
I've got a python script that sets a variable:
notes_eo_counter = 0
And then defines a function:
def printNotesRow(columns): .... ... This has worked just fine in several scripts, but in this particular one I'm gettin an error:
Error Type: UnboundLocalError Error Value: local variable 'notes_eo_counter' referenced before assignment
The problem is that you write to the variable: Variables from an outer scope are read automatically. However, as soon as you assign to a variable, you must declare it as from the outer scope. Please read the Python reference manual for more information. Dieter
participants (3)
-
Chris Withers -
D. Rick Anderson -
Dieter Maurer