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?

Community
  • 1
  • 1
Anirudh Roy
  • 361
  • 1
  • 2
  • 10
  • 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. – Willem Van Onsem Mar 03 '17 at 16:40

4 Answers4

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 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? – Anirudh Roy Mar 03 '17 at 17:26
  • 1
    @AnirudhRoy: indeed and python will look out of the function scope for a variable with the same name. – Willem Van Onsem Mar 03 '17 at 17:28
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

shree
  • 11
  • 1
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
{}
Kelvin
  • 1,357
  • 2
  • 11
  • 22
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.

sangheestyle
  • 1,037
  • 2
  • 16
  • 28
  • 1
    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 http://stackoverflow.com/questions/42023636/defining-variable-after-assigning-them-value-in-python-2-7 – Anirudh Roy Mar 03 '17 at 17:30