#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    UnboundLocalError sometimes appears, sometimes doesn't...


    Hi there, I'm new to Python, (have some C background) and think it's great for the most part, but there's some things I'm running across that are absolutely driving me up the wall. One of these things is I've noticed that error messages sometimes appear, and sometimes don't. For example, I have the following function:

    Code:
    def get_req_square(x, y, coordList):
        w = 0
        for i in range(0, 9):
            for j in range(0, 9):
                if x == j and y == i:
                    gSquare = coordList[w]
                else:
                    w += 1
        return gSquare
    This function gets information about a square on a grid, kind of like a chessboard. Now, sometimes when I run the program I'll get the error - 'unboundlocalerror: 'gSquare' referenced before assignment. Other times, the program runs fine, with no complaints. If I do get the error, I can resolve it doing something like this:

    Code:
    def get_req_square(x, y, coordList):
        w = 0
        gSquare = coordList[0]
        for i in range(0, 9):
            for j in range(0, 9):
                if x == j and y == i:
                    gSquare = coordList[w]
                else:
                    w += 1
        return gSquare
    But the thing is, how can it sometimes produce an error and sometimes not? Shouldn't a programming language produce the same error everytime if it has an issue? I'm worried that the further I go with Python, there will be 'time bombs' set up, to where all of a sudden the program that has worked great for the past month all of a sudden will crash because it 'feels like it'. Any info anyone has is appreciated, thanks!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    For God's sake, irontoad, if you had written the same program in c you'd have gotten no errors when things went wrong. Instead you'd have returned an uninitialized value---which is worse.

    Obviously, in your program if one of x or y weren't in the set(range(9)) then gSquare isn't assigned a value, the name doesn't exist in the function scope when you try to return it.

    Python saved you! The problem with your program lies elsewhere.

    Comments on this post

    • Dietrich agrees : wisely said
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    I rewrote your function so that it will never raise this error, and is 81 times faster.
    Code:
    def get_req_square(x, y, coordList):
        return coordList[9*x + y]
    In any case, Python will only give you an UnboundLocalError when you actually try to access an unbound local. Python does very little static analysis of your program; the fact that it can potentially attempt to return a value that doesn't exist is not something it notices. Tools such as PyLint or PyFlakes do that kind of checking; they may be useful to you.

    Comments on this post

    • b49P23TIvg agrees : Other errors are likely. "can't multiply by None", IndexError, Item must be integral.
    Last edited by Nyktos; May 20th, 2013 at 01:52 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Sorry to bump, but thank you all for your input on this.

IMN logo majestic logo threadwatch logo seochat tools logo