describes the
compile () function to compile a string into bytecode.
Syntax
the following is the syntax of the compile () method:
compile (source, filename, mode [, flags [, dont_inherit]])
parameter
source-- string or AST (Abstract Syntax Trees) object.
filename-the name of the code file, passing some recognizable values if the substitution code is not read from the file.
mode-- specifies the type of compiled code. Can be specified as exec, eval, single.
flags-variable scope, local namespace, and, if provided, any mapping object.
flags and dont_inherit are flags used to control the compilation of source code
return value
returns the result of expression execution.
>>>str = "for i in range(0,10): print(i)"
>>> c = compile(str,"","exec") -sharp
>>> c
<code object <module> at 0x10141e0b0, file "", line 1>
>>> exec(c)
0
1
2
3
4
5
6
7
8
9
>>> str = "3 * 4 + 5"
>>> a = compile(str,"","eval")
>>> eval(a)
17
will this kind of precompilation be faster? You can still what?
I just need to exec and eval strings directly. Why do I have to do so much?
or does the intermediate variable c obtained by compile have some use?
guess if a string needs to exec and eval multiple times, so that it can be called repeatedly with only one compilation, avoiding repeated compilation.
or is there something more useful?