By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Unexpected global variable behaviour

P: n/a
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.

Aug 25 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
RgeeK wrote:
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>

Aug 25 '08 #2

P: n/a
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.


Fredrik Lundh wrote:
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>
Aug 26 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.