[issue38331] Exec not recognizing global variables inside function

2019-10-05 Thread Terry J. Reedy
Terry J. Reedy added the comment: Ronald is correct, and for the reason given. Python functions are lexically scoped, not dynamically scoped, and you are expecting the latter. The exec global and local namespaces are used to resolve identifier in the code you pass. Your first example

[issue38331] Exec not recognizing global variables inside function

2019-10-02 Thread Huyston
Huyston added the comment: Ronald Oussoren, I understand your comment and it makes sense in the module perspective. However, when using 'exec', we are explicitly passing a globals dict (and locals) that the target code should consider. As the documentation states: "If globals and locals

[issue38331] Exec not recognizing global variables inside function

2019-10-01 Thread Ronald Oussoren
Ronald Oussoren added the comment: I don't think this is a bug. def func(): print(var) This captures the globals at the time of definition, that is "global" references are resolved using the func.__globals__ attribute which is the globals dictionary at the time the function is created

[issue38331] Exec not recognizing global variables inside function

2019-10-01 Thread Krishna Oza
Krishna Oza added the comment: adding self to nosy list. -- nosy: +Krishna Oza ___ Python tracker ___ ___ Python-bugs-list mailing

[issue38331] Exec not recognizing global variables inside function

2019-09-30 Thread Huyston
New submission from Huyston : This seems like a bug for me, forgive me if its not. Consider the following script: def func(): print(var) my_globals = {'func':func,'var':14} exec("print(var);func()",my_globals,my_globals) This is the output: 14 Traceback (most recent call last): File