0
A=[]

def main():
    global A
    A=[1,2,3,4,5]
    b()

def b():
    if(len(A)>0):
        A=[7,8,9]
    else:
        if(A[3]==4):
            A.remove(2)

main()  

This code gives error in line A.remove(2) giving reason:"UnboundLocalError: local variable 'A' referenced before assignment"

but A list is global and for sure it has been initialized in main() function.Please explain why this is giving error?

As A has been initialized again in b function, will this cause error?

6
  • Working just fine for me, on Python 3.4.3 and Python 2.7.12. Commented Oct 17, 2016 at 3:20
  • 1
    Can't reproduce. Working fine for me, Python 3.5. Is this your real code? Because maybe there is something in your real code you think isn't causing a problem that actually is. Commented Oct 17, 2016 at 3:20
  • working on python 2.7, 3.5 Commented Oct 17, 2016 at 3:22
  • 1
    this is not my real code, i think this will work, will update the question in few minutes Commented Oct 17, 2016 at 3:23
  • please check code again, it gives error. Commented Oct 17, 2016 at 3:32

2 Answers 2

2

The reason you are getting this is because when you performed this assignment in your function:

A = [7, 8, 9]

The interpreter will now see A as a locally bound variable. So, what will happen now, looking at this condition:

if(len(A)>0):

Will actually throw your first UnboundLocalError exception, because, due to the fact, as mentioned, you never declared global A in your b function and you also created a locally bound variable A = [7, 8, 9], you are trying to use a local A before you ever declared it.

You actually face the same issue when you try to do this:

A.remove(2)

To solve this problem with respect to your code, simply declare the global A back in your b() function.

def b():
    global A
    if(len(A)>0):
        A=[7,8,9]
    else:
        if(A[3]==4):
            A.remove(2)

However, the better, ideal and recommended way to do this is to not use global and just pass the arguments to your function

A = [1, 2, 3, 4]


def main(list_of_things):
    # do whatever operations you want to do here
    b(list_of_things)

def b(list_of_things):
    # do things with list_of_things

main(A)
Sign up to request clarification or add additional context in comments.

1 Comment

@AndrewLi added some clarity.
0

You must declare a variable global in any function that assigns to it.

def b():
    global A
    if some_condition:
        A.append(6)
    else:
        A.remove(2)

Without declaring A global within the scope of b(), Python assumes that A belongs in the b() namespace. You can read it, but cannot edit it (any changes made will not persist to the true A.

You're allowed to set A to something inside the function A = ... will work, but only within the scope of the function.

If you try to mutate A inside of the function without defining A in your function's namespace, then Python will tell you that you are trying to mutate a variable outside of your purview with UnboundLocalError

By declaring A global, the interpreter knows that you mean to edit the global variable.

I'm assuming that you meant for your code to look something like this (Otherwise, it runs fine)

A=[]

if __name__ == '__main__':
    def main():
        global A
        A=[1,2,3,4,5]
        b()

    def b():
        global A
        if some_condition:
            A=[7,8,9]
        else:
            A.remove(2)
        print A

    main()
    print A

print A

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.