Unexpected global variable behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RgeeK

    Unexpected global variable behaviour

    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:

    UnboundLocalErr or: 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.

  • Fredrik Lundh

    #2
    Re: Unexpected global variable behaviour

    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:
    >
    UnboundLocalErr or: 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.


    "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:



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

    </F>

    Comment

    • RgeeK

      #3
      Re: Unexpected global variable behaviour

      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:

      >
      "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:
      >

      >
      def OnOutfileButton (self,evt):
      global outXMLfile # flag variable as global
      fPath, fName = os.path.split(f ullName)
      print "Selected output file:", fName
      outXMLfile = fName
      ...
      >
      </F>
      >

      Comment

      Working...