1

this code gives an 'Unbound Error' (discussed in this query Python variable scope error)

x=3
def f():
    print x
    x+=3

The reason for this (as given in the answer) is that once assignment operator has been used 'x' become a local variable and since 'x' does not have a value attached to it one cannot increase it by 3. But check out this code

x=3
def f():
    print x
    x=3

This time it doesn't seem that 'x' does have a value and hence there shouldn't be any problem, but the same error occurs.

UnboundLocalError: local variable 'x' referenced before assignment

If python has already created a local variable 'x' after reading the statement 'x=3' then why does it not print 'x'?

It is also interesting to note here that this code produces no error

x=3
def f():
    print x
    x

the out being '3' (when f() is called)

This confuses me a lot, isn't this time too 'x' being declared inside 'f()' then shouldn't python add this 'x' to its list of local variable?

1
  • Python compiles a list of variable names you use inside the function. Here x will be used so it already "reserved" space for it, but it is unassigned. Commented Mar 3, 2017 at 16:40

4 Answers 4

2

Well the question to which you link clearly states that:

Python treats variables in functions differently depending on whether you assign values to them from within the function or not.

So in the first two examples you assign to a variable x - regardless whether you do that before or after the print statement - so it means there is a local variable x.

In your last example you do not assign to x: x is not an assignment, only x = (or x +=, etc.) are assignments. So it is an expression. Therefore there is in the last example no local variable x and the one out of the function scope is used.

Sign up to request clarification or add additional context in comments.

2 Comments

So it means that if I dont use an assignment operator inside the function definition then that variable will not be a local variable like in the 3rd case?
@AnirudhRoy: indeed and python will look out of the function scope for a variable with the same name.
1

UnboundLocalError: local variable 'x' referenced before assignment

this error occurs when the interpreter fails to find that specific variable(x in this case) because of its scope. def f(): print x x=3 in the above code, the variable scope is correct but the interpreter reads the code line by line and due to that it will cause this error

Comments

0

The scope of x is outside of f()

The print works without the assignment because it assumes that you are referring to the externally scoped x. If you try to assign it later, it says "hey, he must mean this one and thats not right"

x=3
def f():
   print x
   print locals()


f()
3
{}

Comments

0

Other guys already show us why. Beside, you might want to see this:

x = 3

def f():
    global x
    print(x)
    x += 3

Compare to local variable, you can use global for some reasons.

1 Comment

Well I certainly know that if I want to change the 'x' outside the function I should use global, however I was attempting to know how exactly python interprets the code and goes through the execution. Like the question here stackoverflow.com/questions/42023636/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.