Unexpected global variable behaviour

R

RgeeK

I'm seeing something which make me think I'm missing something about
how global var's behave. I've defined a global string, right at the
start of my .py file.

outXMLfile = "abc"

I define a class and do a bunch of stuff below that. Then I have
another class, and in it, there is a method 'def' that has:

def OnOutfileButton(self,evt):
(fPath, fName)=os.path.split(fullName)
print "Selected output file: " + fName
outXMLfile = fName

print "output file: " + outXMLfile

Print statements in random other places in the project, show outXMLfile
prints as "abc" however, in this def, it comes out as the same as fName
(e.g. "myfile.xml") If the print line for outXMLfile is before the
assignment to fName, it throws the error:

UnboundLocalError: local variable 'outXMLfile' referenced before
assignment

If I remove the line that says "outXMLfile = fName"
then the print statement gives me the value "abc"

What am I missing? When I assign a value to update my global variable,
it becomes a local variable. If I don't try to update it, it stays
global. I assume it's acting like a constant, though I have a couple of
global lists and I seem to be able to append to them okay.

I guess I can move the variable into the parent class and get at it that
way, but want to understand the error of my ways...

Ross.
 
F

Fredrik Lundh

RgeeK said:
I'm seeing something which make me think I'm missing something about
how global var's behave. I've defined a global string, right at the
start of my .py file.

outXMLfile = "abc"

I define a class and do a bunch of stuff below that. Then I have
another class, and in it, there is a method 'def' that has:

def OnOutfileButton(self,evt):
(fPath, fName)=os.path.split(fullName)
print "Selected output file: " + fName
outXMLfile = fName

print "output file: " + outXMLfile

Print statements in random other places in the project, show outXMLfile
prints as "abc" however, in this def, it comes out as the same as fName
(e.g. "myfile.xml") If the print line for outXMLfile is before the
assignment to fName, it throws the error:

UnboundLocalError: local variable 'outXMLfile' referenced before
assignment

If I remove the line that says "outXMLfile = fName"
then the print statement gives me the value "abc"

What am I missing? When I assign a value to update my global variable,
it becomes a local variable. If I don't try to update it, it stays
global. I assume it's acting like a constant, though I have a couple of
global lists and I seem to be able to append to them okay.

http://docs.python.org/ref/naming.html

"If a name binding operation occurs anywhere within a code block, all
uses of the name within the block are treated as references to the
current block. This can lead to errors when a name is used within a
block before it is bound."

to fix this, use the global directive:

http://docs.python.org/ref/global.html

def OnOutfileButton(self,evt):
global outXMLfile # flag variable as global
fPath, fName = os.path.split(fullName)
print "Selected output file:", fName
outXMLfile = fName
...

</F>
 
R

RgeeK

Thanks a lot Fredrik,

A big wave of deja vu came over me as I read your note. That bit me once
before, and was my only previous run-in with the 'global' directive
which clearly didn't stick with me. :(

Thanks for the concise, helpful reply. The universe makes sense again.

-Ross.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top