Hello friends,

I have a question to ask.

If you define the following cases:

Case 1:
-----------
x = 1
def f():
    print(x)

Case 2:
-----------
x = 1
def f():
    print(x)
    x = 5
    print(x)


If you call the function f, in case 1 there won't be a problem, It will
print 1. But in case 2, it will give an error "UnboundLocalError: local
variable 'x' referenced before assignment"...One would think that in case
2, it should have printed 1 and 5. Any one could explain what is happening
here? Why does the interpreter get confused in the 2nd Case?

Thanks.

Regards,
Ranjith
_______________________________________________
Chennaipy mailing list
Chennaipy@python.org
https://mail.python.org/mailman/listinfo/chennaipy

Reply via email to