Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f574aa

Browse files
committed
Added back the description of the exec statement. It appears that I
accidentally cut it out when removing the access statement! Added a paragraph on __builtins__ and other possible manipulations of the key space of the dictionaries. Added some index entries.
1 parent 9f2b524 commit 5f574aa

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Doc/ref/ref6.tex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,41 @@ \section{The \keyword{global} statement} \label{global}
509509
\bifuncindex{eval}
510510
\bifuncindex{execfile}
511511
\bifuncindex{compile}
512+
513+
\section{The {\tt exec} statement} \label{exec}
514+
\stindex{exec}
515+
516+
\begin{verbatim}
517+
exec_stmt: "exec" expression ["in" expression ["," expression]]
518+
\end{verbatim}
519+
520+
This statement supports dynamic execution of Python code. The first
521+
expression should evaluate to either a string, an open file object, or
522+
a code object. If it is a string, the string is parsed as a suite of
523+
Python statements which is then executed (unless a syntax error
524+
occurs). If it is an open file, the file is parsed until EOF and
525+
executed. If it is a code object, it is simply executed.
526+
527+
In all cases, if the optional parts are omitted, the code is executed
528+
in the current scope. If only the first expression after \keyword{in}
529+
is specified, it should be a dictionary, which will be used for both
530+
the global and the local variables. If two expressions are given,
531+
both must be dictionaries and they are used for the global and local
532+
variables, respectively.
533+
534+
As a side effect, an implementation may insert additional keys into
535+
the dictionaries given besides those corresponding to variable names
536+
set by the executed code. For example, the current implementation
537+
may add a reference to the dictionary of the built-in module
538+
\module{__builtin__} under the key \code{__builtins__} (!).
539+
\ttindex{__builtins__}
540+
\refbimodindex{__builtin__}
541+
542+
Hints: dynamic evaluation of expressions is supported by the built-in
543+
function \function{eval()}. The built-in functions
544+
\function{globals()} and \function{locals()} return the current global
545+
and local dictionary, respectively, which may be useful to pass around
546+
for use by \keyword{exec}.
547+
\bifuncindex{eval}
548+
\bifuncindex{globals}
549+
\bifuncindex{locals}

0 commit comments

Comments
 (0)