[Chennaipy] Python interpreter

venkata krishnan ve_krish at yahoo.com
Sun Jul 3 13:52:43 EDT 2016


Dear Ranjit,

It is not the interpreter alone in action and it flutters. From my learning by going through the articles below.
It works in conjunction with Symbol table to determine the scope of each name binding. So, before it reaches the print(x) in case 2. It has the symbol table prepared for the function/block. When the LEGB scoping rule applied. It finds that, you have already an assignment operator for x in that scope. Thus, it treats it as an UnboundLocalError. Where as in case 1. The symbol table would not have any local scope/assignment for x.


Please go through below links(articles) from below. Has a very good explanation of "UnboundLocalError".
1. http://eli.thegreenplace.net/2011/05/15/understanding-unboundlocalerror-in-python/
2. Symbol table construction in Python's compiler
    Part1: http://eli.thegreenplace.net/2010/09/18/python-internals-symbol-tables-part-1/

    Part2: http://eli.thegreenplace.net/2010/09/20/python-internals-symbol-tables-part-2/


Thanks
Venkat


More information about the Chennaipy mailing list