local variable referenced before assignment

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

    local variable referenced before assignment


    Quick question, probably quite a simple matter. Take the follow start of
    a method:


    def review(filesNee dingReview):

    for item in filesNeedingRev iew:
    (tightestOwner, logMsg) = item

    if (logMsg != None):
    for logInfo in logMsg.changed_ paths:


    This generates the error:

    UnboundLocalErr or: local variable 'logMsg' referenced before assignment

    I thought I'd assigned it in the "(tightestOwner , logMsg) = item" line -
    so in the python interpreter complaining about the fact this assignment
    might not go well?

    Thanks!
  • A.T.Hofkamp

    #2
    Re: local variable referenced before assignment

    On 2007-10-25, Pete Bartonly <none@try.inval idwrote:
    >
    Quick question, probably quite a simple matter. Take the follow start of
    a method:
    >
    >
    def review(filesNee dingReview):
    >
    for item in filesNeedingRev iew:
    (tightestOwner, logMsg) = item
    >
    if (logMsg != None):
    for logInfo in logMsg.changed_ paths:
    >
    >
    This generates the error:
    >
    UnboundLocalErr or: local variable 'logMsg' referenced before assignment
    This should work, are you sure you didn't make a typo in one of the names?

    Another way to make this fail would be when the if-condition is outside
    the loop (is the indentation correct in your code?).

    A short demontration:
    >>def r(fnr):
    .... for item in fnr:
    .... w,m = item
    .... if m == 2:
    .... print w
    ....
    >>fnr = [(1,2), (3,4)]
    >>r(fnr)
    1

    With respect to compactness and style, you can move your multi-assignment
    statement in the for loop, as in

    for tightestOwner, logMsg in filesNeedingRev iew:

    Also, brackets around conditions (in the if) are not needed, and comparing
    against None is usually done with 'is' or 'is not' instead of '==' or '!='.
    The result is then

    if logMsg is not None:

    I thought I'd assigned it in the "(tightestOwner , logMsg) = item" line -
    so in the python interpreter complaining about the fact this assignment
    might not go well?
    No, you'd get an error at that point in that case.


    Sincerely,
    Albert

    Comment

    • Peter Otten

      #3
      Re: local variable referenced before assignment

      Pete Bartonly wrote:
      Quick question, probably quite a simple matter. Take the follow start of
      a method:
      >
      >
      def review(filesNee dingReview):
      >
      for item in filesNeedingRev iew:
      (tightestOwner, logMsg) = item
      >
      if (logMsg != None):
      for logInfo in logMsg.changed_ paths:
      >
      >
      This generates the error:
      >
      UnboundLocalErr or: local variable 'logMsg' referenced before assignment
      >
      I thought I'd assigned it in the "(tightestOwner , logMsg) = item" line -
      so in the python interpreter complaining about the fact this assignment
      might not go well?
      My crystal ball tells me that you are not posting the actual code where
      for... and if... are indented to the same level. This triggers the error
      when review() is called with an empty sequence.

      Please remember to copy and paste both code and traceback next time.

      Peter

      Comment

      • Arnaud Delobelle

        #4
        Re: local variable referenced before assignment

        On Oct 25, 10:02 am, Pete Bartonly <n...@try.inval idwrote:
        Quick question, probably quite a simple matter. Take the follow start of
        a method:
        >
        def review(filesNee dingReview):
        >
        for item in filesNeedingRev iew:
        (tightestOwner, logMsg) = item
        >
        if (logMsg != None):
        for logInfo in logMsg.changed_ paths:
        >
        This generates the error:
        >
        UnboundLocalErr or: local variable 'logMsg' referenced before assignment
        Check your indentation?
        Seems to me that you might really have:

        def review(...):
        for ...:
        ....
        if (logMsg...):
        ....

        HTH

        --
        Arnaud


        Comment

        • Tim Williams

          #5
          Re: local variable referenced before assignment

          On 25/10/2007, A.T.Hofkamp <hat@se-162.se.wtb.tue. nlwrote:
          On 2007-10-25, Pete Bartonly <none@try.inval idwrote:
          Also, brackets around conditions (in the if) are not needed, and comparing
          against None is usually done with 'is' or 'is not' instead of '==' or '!='.
          The result is then
          >
          if logMsg is not None:
          Or just
          >> if logMsg:
          do_something()

          :)

          Comment

          • A.T.Hofkamp

            #6
            Re: local variable referenced before assignment

            On 2007-10-25, Tim Williams <tdwdotnet@gmai l.comwrote:
            On 25/10/2007, A.T.Hofkamp <hat@se-162.se.wtb.tue. nlwrote:
            >On 2007-10-25, Pete Bartonly <none@try.inval idwrote:
            >
            >Also, brackets around conditions (in the if) are not needed, and comparing
            >against None is usually done with 'is' or 'is not' instead of '==' or '!='.
            >The result is then
            >>
            >if logMsg is not None:
            >
            Or just
            >
            >>> if logMsg:
            do_something()
            >
            >:)
            That is not the same.

            If logMsg is 0, False, or empty string, the second variant
            would be False and not True.

            Albert

            Comment

            • Pete Bartonly

              #7
              Re: local variable referenced before assignment

              A.T.Hofkamp wrote:
              On 2007-10-25, Pete Bartonly <none@try.inval idwrote:
              >Quick question, probably quite a simple matter. Take the follow start of
              >a method:
              >>
              >>
              >def review(filesNee dingReview):
              >>
              > for item in filesNeedingRev iew:
              > (tightestOwner, logMsg) = item
              >>
              > if (logMsg != None):
              > for logInfo in logMsg.changed_ paths:
              >>
              >>
              >This generates the error:
              >>
              > UnboundLocalErr or: local variable 'logMsg' referenced before assignment
              >
              This should work, are you sure you didn't make a typo in one of the names?
              Nope, the above is verbatim. This is why I'm so confused. It should
              work! I'm editing in emacs, and the indents are tab chars. I've
              re-indented the indents using 'tab' key - same result.

              The entire error output is this:

              Traceback (most recent call last):
              File "checkCode. py", line 602, in ?
              analyseFiles(te mpDir)
              File "checkCode. py", line 448, in analyseFiles
              analyseFilesInA Repos(startDir, f)
              File "checkCode. py", line 590, in analyseFilesInA Repos
              makeReport(proj Name, filesNeedingRev iew, filesFailedRevi ew)
              File "checkCode. py", line 422, in makeReport
              for logInfo in logMsg.changed_ paths:
              UnboundLocalErr or: local variable 'logMsg' referenced before assignment

              I'm rather stuck at what to try next!

              thanks.
              Pete






              >
              Another way to make this fail would be when the if-condition is outside
              the loop (is the indentation correct in your code?).
              >
              A short demontration:
              >>>def r(fnr):
              ... for item in fnr:
              ... w,m = item
              ... if m == 2:
              ... print w
              ...
              >>>fnr = [(1,2), (3,4)]
              >>>r(fnr)
              1
              >
              With respect to compactness and style, you can move your multi-assignment
              statement in the for loop, as in
              >
              for tightestOwner, logMsg in filesNeedingRev iew:
              >
              Also, brackets around conditions (in the if) are not needed, and comparing
              against None is usually done with 'is' or 'is not' instead of '==' or '!='.
              The result is then
              >
              if logMsg is not None:
              >
              >
              >I thought I'd assigned it in the "(tightestOwner , logMsg) = item" line -
              >so in the python interpreter complaining about the fact this assignment
              >might not go well?
              >
              No, you'd get an error at that point in that case.
              >
              >
              Sincerely,
              Albert

              Comment

              • Pete Bartonly

                #8
                Re: local variable referenced before assignment

                A.T.Hofkamp wrote:
                On 2007-10-25, Pete Bartonly <none@try.inval idwrote:
                >Quick question, probably quite a simple matter. Take the follow start of
                >a method:
                With respect to compactness and style, you can move your multi-assignment
                statement in the for loop, as in
                [snip]

                Btw, thanks for the tips on style too!
                Pete

                Comment

                • Pete Bartonly

                  #9
                  Re: local variable referenced before assignment

                  Peter Otten wrote:
                  Pete Bartonly wrote:
                  >
                  >Quick question, probably quite a simple matter. Take the follow start of
                  >a method:
                  >>
                  >>
                  >def review(filesNee dingReview):
                  >>
                  > for item in filesNeedingRev iew:
                  > (tightestOwner, logMsg) = item
                  >>
                  > if (logMsg != None):
                  > for logInfo in logMsg.changed_ paths:
                  >>
                  >>
                  >This generates the error:
                  >>
                  > UnboundLocalErr or: local variable 'logMsg' referenced before assignment
                  >>
                  >I thought I'd assigned it in the "(tightestOwner , logMsg) = item" line -
                  >so in the python interpreter complaining about the fact this assignment
                  >might not go well?
                  >
                  My crystal ball tells me that you are not posting the actual code where
                  for... and if... are indented to the same level.
                  I am! See my other reply just now.

                  Here it the code again, directly cut and pasted from emacs:

                  for item in filesNeedingRev iew:
                  (tightestOwner, logMsg) = item

                  if (logMsg != None):
                  for logInfo in logMsg.changed_ paths:
                  if (not tightestOwner in emailListForRev iewers):
                  emailListForRev iewers.append(t ightestOwner)




                  This triggers the error
                  when review() is called with an empty sequence.
                  >
                  Please remember to copy and paste both code and traceback next time.
                  Sorry 'bout that. The traceback I forgot is:

                  Traceback (most recent call last):
                  File "checkCode. py", line 599, in ?
                  analyseFiles(te mpDir)
                  File "checkCode. py", line 445, in analyseFiles
                  analyseFilesInA Repos(startDir, f)
                  File "checkCode. py", line 587, in analyseFilesInA Repos
                  makeReport(proj Name, filesNeedingRev iew, filesFailedRevi ew)
                  File "checkCode. py", line 419, in makeReport
                  for logInfo in logMsg.changed_ paths:
                  UnboundLocalErr or: local variable 'logMsg' referenced before assignment


                  Pete

                  Comment

                  • Pete Bartonly

                    #10
                    Re: local variable referenced before assignment

                    Pete Bartonly wrote:
                    >
                    Quick question, probably quite a simple matter. Take the follow start of
                    a method:
                    >
                    >
                    def review(filesNee dingReview):
                    >
                    for item in filesNeedingRev iew:
                    (tightestOwner, logMsg) = item
                    >
                    if (logMsg != None):
                    for logInfo in logMsg.changed_ paths:
                    >
                    >
                    This generates the error:
                    >
                    UnboundLocalErr or: local variable 'logMsg' referenced before assignment
                    >
                    I thought I'd assigned it in the "(tightestOwner , logMsg) = item" line -
                    so in the python interpreter complaining about the fact this assignment
                    might not go well?
                    >
                    Thanks!
                    Argh! Mea culpa everyone!
                    Turns out that there is a similar few lines of code later on in the
                    code. I was confusing which was which. (I was using meta-x goto-line but
                    then looking at the wrong but somehow, as it appeared on the same screen
                    in emacs.)
                    So the error was occuring at a similar line in the code, but one at
                    whihc logMsg *wasn't* being set beforehand.

                    Sorry all, thanks for your help, appreciate it!
                    Pete

                    Comment

                    Working...