[Chennaipy] Python interpreter

ranjith pillay ammaranjith at gmail.com
Fri Jul 1 07:27:48 EDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chennaipy/attachments/20160701/8073650d/attachment.html>


More information about the Chennaipy mailing list