Copyright (C) 2000-2012 |
GNU Info (python2.1-ref.info)exec statementThe `exec' statement ==================== exec_stmt: "exec" expression ["in" expression ["," expression]] This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed. In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after `in' is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively. As a side effect, an implementation may insert additional keys into the dictionaries given besides those corresponding to variable names set by the executed code. For example, the current implementation may add a reference to the dictionary of the built-in module `__builtin__' under the key `__builtins__' (!). *Programmer's hints:* dynamic evaluation of expressions is supported by the built-in function `eval()'. The built-in functions `globals()' and `locals()' return the current global and local dictionary, respectively, which may be useful to pass around for use by `exec'. Also, in the current implementation, multi-line compound statements must end with a newline: `exec "for v in seq:\n\tprint v\n"' works, but `exec "for v in seq:\n\tprint v"' fails with `SyntaxError'. automatically generated by info2www version 1.2.2.9 |