Enclosure functions

If we write x="local’ inside outer() and use print(x) inside inner(), it works. But when we write x= 5 and try x+=10 inside inner(), it shows error, why this happens?

hey @nikhil_0406 ,
its because that , if we directly use print command in inner then python will take the previous value of x to be as global .
But when we try to update it , then it doesn’t do so.
it starts to find what is x and finds its value , hence it is not initialized in its scope, it raises error.

I hope this helps.

1 Like

but when we tried printing, that time too the string was not in its scope.

hey @nikhil_0406 ,
yes you are right.
But that’s the mystery , when we just directly printed the value , it got that particular value of x , but when we tried to update it , then it raised the error.
Its just because the rules designed in compilation of python and we can’t do anything in it.

1 Like

yeah thanks, I understand now.

Good to hear this .
I request you to kindly mark this doubt as resolved and do provide you valuable feedback .

Thank You and Happy Learning :slightly_smiling_face:.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.