effect of "exec" on local scope
The issue came up while trying to get some Sympy code running on CLPython. class C: exec "a = 3" print locals() 1. Is it guaranteed that class C gets an attribute "a", i.e. that the locals printed include {'a': 3}? 2. It it (also) guaranteed if it were in a function scope? The complete syntax of exec is: exec CODE in Y, Z where Y, Z are optional. The documentation of "exec" says "if the optional parts are omitted,the code is executed in the current scope." There are at least two different interpretations: a. The code is executed in the current class scope, so the assignment must have an effect on the class scope. b. The scope defaults to the local scope, by which is meant the mapping returned by locals(), and of locals() the documentation says that changes made to it may not influence the interpreter. (The documentation of exec suggests using globals() and locals() as arguments to exec, which seems hint at this interpretation.) The relevant documentation: exec: http://docs.python.org/reference/simple_stmts.html#grammar-token-exec_stmt locals: http://docs.python.org/library/functions.html#locals - Willem
participants (5)
-
Guido van Rossum
-
Nick Coghlan
-
Ondrej Certik
-
Terry Reedy
-
Willem Broekema