Feb-24-2022, 09:06 PM
While answering a question on the forum I wrote an example where I knew the outcome, but I don't understand the reason for the outcome.
I know that Python parses the function to generate bytecodes and this must include creating symbols for variables that the function will use. Is Python using this "symbol table" when looking for variables instead of the locals dictionary? Is there a way for me to see these symbols? Is there something I could read that describes how UnboundLocalError works?
check = 0 def sample_fcn() print(check) check = 1This generates an UnboundLocalError when doing the "print(check)". My question is "How does Python know there is a local variable named "check"?
I know that Python parses the function to generate bytecodes and this must include creating symbols for variables that the function will use. Is Python using this "symbol table" when looking for variables instead of the locals dictionary? Is there a way for me to see these symbols? Is there something I could read that describes how UnboundLocalError works?