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. 2 1630
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>
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gerhard Esterhuizen |
last post by:
Hi,
I am observing unexpected behaviour, in the form of a corrupted class
member access, from a simple C++ program that accesses an attribute
declared in a virtual base class via a chain of...
|
by: Estella |
last post by:
Hello, I wrote a function called eat_path() to split a string into
components
e.g. /a/b/c ==> namePtr = a,namePtr = a, namePtr = c
// Global variable
char *namePtr = {0};
int n; /*number of...
|
by: ankisharma |
last post by:
Hi all
At many places I have seen that programmers pass global variables
to functions in c. I am not able to figure out why they do so. need
some clues on this. somewhere i heard that this...
|
by: didier.doussaud |
last post by:
I have a stange side effect in my project :
in my project I need to write "gobal" to use global symbol :
....
import math
....
def f() :
global math # necessary ?????? else next line...
|
by: zeecanvas |
last post by:
Hi,
First of all: Yes, I know global variables are bad, but I've a huge
amount of legacy code, and I've to maintain it _as_is_.
I'm maintaining a big program. I moved all (program-wide scope)...
|
by: Roman Ziak |
last post by:
Hello,
there were times when I used to be looking for a way to access
JavaScript Global object similar to those found in VBScript or PHP
($GLOBALS). At present this has only academic value for...
|
by: subramanian100in |
last post by:
Suppose I declare a global variable
int g;
in two different files say a.c which has main() and b.c
When I compile them to build an executable under gcc in Redhat Linux
with the command
...
|
by: gu |
last post by:
hi to all!
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course!...
|
by: teddysnips |
last post by:
Weird.
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |