What is the use of the Python compile function?

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?

Dec.22,2021

for the example code, there is no improvement. It is equivalent to splitting exec into compile and introducing an assignment and read operation in the middle of exec,.
to improve performance, the code of compile must be very complex. Compile optimization time is longer than assignment and read operations, generally complex calculations and large files

.

in addition, you can think of several uses:
1. Code analysis, which is equivalent to the function of gcc assembly, makes it easy to optimize and analyze the relationship between python bytecode generation and source code
2. Code pre-check. Exec and eval are generally used to embed code into user programs for extension. At this time, compile can be used to pre-check the code. If the format is incorrect, it cannot be compiled

.

you can also take a look at
python-built-in-function-compile-what-is-it-used-for" rel=" nofollow noreferrer "> https://stackoverflow.com/que.

here.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e8f035-46fe7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e8f035-46fe7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?