Python 内置函数“编译”。它是干什么用的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22443939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python built-in function "compile". What is it used for?
提问by demo.b
I came across a built-in function compiletoday. Though i read the documentation but still do not understand it's usage or where it is applicable. Please can anyone explain with example the use of this function. I will really appreciate examples.
我compile今天遇到了一个内置函数。虽然我阅读了文档,但仍然不明白它的用法或适用的地方。请任何人都可以举例说明此功能的使用。我会非常欣赏例子。
From the documentation, the function takes some parameters as shown below.
从文档中,该函数采用一些参数,如下所示。
compile(source, filename, mode[, flags[, dont_inherit]])
采纳答案by BrenBarn
It is not that commonly used. It is used when you have Python source code in string form, and you want to make it into a Python code object that you can keep and use. Here's a trivial example:
它不是那么常用。当您拥有字符串形式的 Python 源代码,并且希望将其变成可以保留和使用的 Python 代码对象时,将使用它。这是一个简单的例子:
>>> codeobj = compile('x = 2\nprint "X is", x', 'fakemodule', 'exec')
>>> exec(codeobj)
X is 2
Basically, the code object converts a string into an object that you can later call execon to run the source code in the string. (This is for "exec" mode; the "eval" mode allows use of evalinstead, if the string contains code for a single expression.) This is not a common task, which is why you may never run across a need for it.
基本上,代码对象将一个字符串转换为一个对象,您可以稍后调用该对象exec来运行该字符串中的源代码。(这适用于“exec”模式;eval如果字符串包含单个表达式的代码,则“eval”模式允许使用。)这不是一项常见任务,这就是为什么您可能永远不会遇到它的需要。
The main use for it is in metaprogramming or embedding situations. For instance, if you have a Python program that allows users to script its behavior with custom Python code, you might use compileand execto store and execute these user-defined scripts.
它的主要用途是元编程或嵌入情况。例如,如果您有一个 Python 程序,允许用户使用自定义 Python 代码编写其行为脚本,您可以使用compile和exec来存储和执行这些用户定义的脚本。
Another reason compileis rarely used is that, like exec, eval, and their ilk, compileis a potential security hole. If you take user code in string form and compile it and later exec it, you could be running unsafe code. (For instance, imagine that in my example above the code was formatYourHardDrive()instead of print x.)
另一个原因compile是很少使用的是一样exec,eval和它们的同类,compile是一个潜在的安全漏洞。如果您以字符串形式获取用户代码并对其进行编译,然后再执行它,那么您可能正在运行不安全的代码。(例如,假设在我上面的示例中,代码formatYourHardDrive()不是print x。)
回答by Jonathon Reinhart
What specifically don't you understand? The documentation explains that it will:
具体什么不明白?该文档解释说它将:
Compile the source into a code or AST object. Code objects can be executed by an
execstatement or evaluated by a call toeval(). source can either be a Unicode string, a Latin-1 encoded string or an AST object. Refer to theastmodule documentation for information on how to work with AST objects.
将源代码编译为代码或 AST 对象。代码对象可以由
exec语句执行或通过调用来评估eval()。source 可以是 Unicode 字符串、Latin-1 编码字符串或 AST 对象。ast有关如何使用 AST 对象的信息,请参阅模块文档。
So it takes python code, and returns on of those two things
所以它需要 python 代码,并返回这两件事
execwill execute the python codeevalwill evaluate an expression, which is less functional thanexecastallows you to navigate the Abstract Syntax Treethat the code generates
exec将执行python代码eval将评估一个表达式,它的功能比execast允许您导航代码生成的抽象语法树
回答by Alex Thornton
compileis a lower level version of execand eval. It does not execute or evaluate your statements or expressions, but returns a code object that can do it. The modes are as follows:
compile是水平较低版本exec和eval。它不会执行或评估您的语句或表达式,而是返回可以执行此操作的代码对象。模式如下:
compile(string, '', 'eval')returns the code object that would have been executed had you doneeval(string). Note that you cannotuse statements in this mode; only a (single) expression is valid. Used for a single expression.compile(string, '', 'exec')returns the code object that would have been executed had you doneexec(string). You can use any number of statements here. Used for an entire module.compile(string, '', 'single')is like theexecmode, but it will ignore everything except for the first statement. Note that anif/elsestatement with its results is considered a single statement. Used for one single statement.
compile(string, '', 'eval')返回如果你完成了将被执行的代码对象eval(string)。请注意,您不能在此模式下使用语句;只有(单个)表达式有效。用于单个表达式。compile(string, '', 'exec')返回如果你完成了将被执行的代码对象exec(string)。您可以在此处使用任意数量的语句。用于整个模块。compile(string, '', 'single')就像exec模式一样,但它会忽略除了第一条语句之外的所有内容。请注意,带有其结果的if/else语句被视为单个语句。用于单个语句。
Take a look that the documentation. There is also an awesome (well, dumbed down) explanation at http://joequery.me/code/python-builtin-functions/#compilewith an excellent example of usage.
看看文档。在http://joequery.me/code/python-builtin-functions/#compile上还有一个很棒的(好吧,简化了)解释,并提供了一个很好的使用示例。

