简体   繁体   English

Python内置函数“编译”。 它是干什么用的?

[英]Python built-in function “compile”. What is it used for?

I came across a built-in function compile today. 我今天遇到了一个内置函数compile 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. 我将非常感谢示例。

From the documentation, the function takes some parameters as shown below. 在文档中,该函数采用一些参数,如下所示。

compile(source, filename, mode[, flags[, dont_inherit]])

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. 当您具有字符串形式的Python源代码并且想要将其变成可以保留和使用的Python代码对象时,将使用它。 Here's a trivial example: 这是一个简单的例子:

>>> 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 exec on to run the source code in the string. 基本上,代码对象将字符串转换为对象,以后您可以调用exec来运行字符串中的源代码。 (This is for "exec" mode; the "eval" mode allows use of eval instead, 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”模式的;如果字符串包含单个表达式的代码,则“ 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 compile and exec to store and execute these user-defined scripts. 例如,如果您有一个Python程序,该程序允许用户使用自定义Python代码编写其行为脚本,则可以使用compileexec来存储和执行这些用户定义的脚本。

Another reason compile is rarely used is that, like exec , eval , and their ilk, compile is a potential security hole. 很少使用compile另一个原因是,像execeval和它们的同类一样, compile是潜在的安全漏洞。 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 .) (例如,假设在我上面的示例中,代码是formatYourHardDrive()而不是print x 。)

compile is a lower level version of exec and eval . compileexeceval的较低版本。 It does not execute or evaluate your statements or expressions, but returns a code object that can do it. 它不会执行或评估您的语句或表达式,但会返回可以执行此操作的代码对象。 The modes are as follows: 模式如下:

  1. compile(string, '', 'eval') returns the code object that would have been executed had you done eval(string) . compile(string, '', 'eval')返回如果完成eval(string)将会执行的代码对象。 Note that you cannot use statements in this mode; 请注意,您不能在这种模式下使用语句。 only a (single) expression is valid. 仅(单个)表达式有效。 Used for a single expression. 用于单个表达式。
  2. compile(string, '', 'exec') returns the code object that would have been executed had you done exec(string) . compile(string, '', 'exec')返回如果执行完exec(string)后将要执行的代码对象。 You can use any number of statements here. 您可以在此处使用任意数量的语句。 Used for an entire module. 用于整个模块。
  3. compile(string, '', 'single') is like the exec mode, but it will ignore everything except for the first statement. compile(string, '', 'single')类似于exec模式,但是它将忽略除第一条语句以外的所有内容。 Note that an if / else statement with its results is considered a single statement. 请注意,带有结果的if / else语句被视为单个语句。 Used for one single statement. 用于一个语句。

Take a look that the documentation . 看看那个文档 There is also an awesome (well, dumbed down) explanation at http://joequery.me/code/python-builtin-functions/#compile with an excellent example of usage. http://joequery.me/code/python-builtin-functions/#compile上也有一个很棒的解释(很好,很简单),其中有一个很好的用法示例。

What specifically don't you understand? 您具体不了解什么? The documentation explains that it will: 该文档说明它将:

Compile the source into a code or AST object. 将源编译为代码或AST对象。 Code objects can be executed by an exec statement or evaluated by a call to eval() . 代码对象可以通过exec语句执行,也可以通过调用eval()进行eval() source can either be a Unicode string, a Latin-1 encoded string or an AST object. 源可以是Unicode字符串,Latin-1编码的字符串或AST对象。 Refer to the ast module documentation for information on how to work with AST objects. 有关如何使用AST对象的信息,请参阅ast模块文档。

So it takes python code, and returns on of those two things 因此,它需要python代码,并返回这两件事

  • exec will execute the python code exec将执行python代码
  • eval will evaluate an expression, which is less functional than exec eval将计算一个表达式,其功能不如exec
  • ast allows you to navigate the Abstract Syntax Tree that the code generates ast允许您浏览代码生成的抽象语法树

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM