[Tutor] strange variable problem

Dave S pythontut at pusspaws.net
Sat May 22 04:21:02 EDT 2004


Alan Gauld wrote:

>>>ftseptr is setup as a global variable, value -1,
>>>yftseptr is setup as a global variable, value 0
>>>      
>>>
>>Ah!  Common gotcha: when using global variables in functions, you
>>    
>>
>may need
>  
>
>>to declare that the variable is 'global' in nature.  For example:
>>    
>>
>
>Is that really the problem here?
>He is not assigning to the variable merely printing it,
>which shouldn't need the global keyword.
>
>However I don't think we have the full story.
>Looking at the traceback:
>
>  
>
>>Traceback (most recent call last):
>>  File "/home/dave/gg/gg.py", line 72, in -toplevel-
>>    gg()
>>  File "/home/dave/gg/gg.py", line 62, in gg
>>    file2datacore2(stockdir+dir+'/'+file,syncerr)
>>  File "/home/dave/gg/gg.py", line 40, in file2datacore2
>>
>>ggdatacore2.add(datalist[i+1],datalist[i+3],...syncerr)
>>  File "/home/dave/gg/ggdatacore2.py", line 30, in add
>>    print ftseptr
>>UnboundLocalError: local variable 'ftseptr' referenced before
>>    
>>
>assignment
>
>we have calls to gg() and file2datacore2 and then add() for
>  
>
Yep its part of a larger program .... :-)

>which we have the code. Plus the program seems to be run from
>a python prompt rather than being executed as a program.
>  
>
Its run via IDLE

>If that is the case a lot depends on how the imports are being
>done because that's when the global variables will be set up.
>Presumably, for the code to get this far, the module gg does
>
>import ggdatacore2
>
>But when is gg imported? Has anything changed between then
>and the toplevel call to gg()?
>
>What happens if you run your program outside the python prompt?
>Does that change the error in any way?
>
>  
>
>>###
>>count = 0
>>def makeNextName():
>>    global count
>>    nextName = "name%s" % count
>>    count = count + 1
>>    return nextName
>>###
>>
>>If the 'global count' declaration in the function is missing, then
>>    
>>
>we'll
>  
>
>>get the same UnboundLocalError that you're receiving in your
>>    
>>
>program, so I
>  
>
>>suspect the same thing is happening there.
>>    
>>
>
>Yes but in this case the error is because of the assignment which
>tries to create a local variable. But Dave's code doesn't assign
>to the variable merely prints it. So it should be OK,
>unless he hasn't shown us the full code...
>
>puzzled,
>
>Alan G.
>  
>
I just read your e-mails this morning after staying up to 2:00 Am to fix 
it ! wish I had downloaded mail earlier :-)

The code I pasted to the group was only a section of a larger program 
calling other scripts, I editied it to what I thought was the problem 
area rather then send an unwieldy e-mail.

 From reading you guys emails, and I think I have had a breakthrough ....

What confused me whas that I could define 2x global variables at the top 
of the script, but I could only print one, the other one generated an 
exception.

adding a global inside def add sorted the problem ....

def add(id,mid,pence,pc,bid,offer,open,high,low,close,volume,syncerr):

   global ftseptr,yftseptr
   # Data cookie - is a list
   # [num consecutive loads,bad data counter,bad cycle counter]
   print yftsept   # Now works AOK :-)
   print ftseptr

However this did not explain why one variable was OK, the other not ...

However further down in def add(...) (err I didn't think this was 
important  :-[ ) I do have a single
ftseptr+=1

I guess this is the same as ftseptr=ftseptr+1, an assignment

So (wing and prayer thinking here), python scanned my script (at 
'compile' time ?), found the ftseptr+=1, treated it as an assignment and 
therefore local without a global declaration, then tried executing add() 
found print ftseptr and said ....

UnboundLocalError: local variable 'ftseptr' referenced before assignment

:-) .... I think so anyhow.

Dave

PS next time I will paste all the code ... even is it is a bit unwieldy !








More information about the Tutor mailing list