UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)

Python treats variables in functions differently depending on whether you assign values to them from inside or outside the function. If a variable is assigned within a function, it is treated by default as a local variable. Therefore, when you uncomment the line, you are trying to reference the local variable c before any value … Read more

How to get local variables updated, when using the `exec` call?

This issue is somewhat discussed in the Python3 bug list. Ultimately, to get this behavior, you need to do: def foo(): ldict = {} exec(“a=3”,globals(),ldict) a = ldict[‘a’] print(a) And if you check the Python3 documentation on exec, you’ll see the following note: The default locals act as described for function locals() below: modifications to … Read more

Why a variable defined global is undefined? [duplicate]

You have just stumbled on a js “feature” called hoisting var myname = “global”; // global variable function func() { alert(myname); // “undefined” var myname = “local”; alert(myname); // “local” } func(); In this code when you define func the compiler looks at the function body. It sees that you are declaring a variable called … Read more