Closed kmindspark closed 4 years ago
@kmindspark Thanks for the report. This is the intended behaviour, as you can see by running the same code with CPython.
It is because exec()
executes code in an environment with a global namespace and a local namespace; in your code, myvar
is bound in the global namespace, and the documentation says that "The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals"
Hi - thanks for the response. I had a one remaining question though.
Why does the global variable, inside the function, when printed, show as \<Javascript undefined>?
Solved by passing globals() to exec. Thank you.
Commit 86eb33e fixes the issue with global
without having to explicitely pass globals()
to exec.
Code:
When I run this inside another brython python function, I get the following error:
When I replace nonlocal with global, I get the following outputs.
I was wondering if there is a bug going on here, and if not, if someone could explain to me why this is the intended behavior.