Python內(nèi)置函數(shù)(12)——compile

英文文檔:
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

Compile the source into a code or AST object. Code objects can be executed by exec() or eval(). source can either be a normal string, a byte string, or an AST object. Refer to the ast module documentation for information on how to work with AST objects.

The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('<string>'
is commonly used).

The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists of a sequence of statements, 'eval' if it consists of a single expression, or 'single' if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than None will be printed).

The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling compile(). If the flagsargument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future statements in effect around the call to compile are ignored.

Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the compiler_flag attribute on the _Feature
instance in the future module.

The argument optimize specifies the optimization level of the compiler; the default value of -1 selects the optimization level of the interpreter as given by -O options. Explicit levels are 0 (no optimization; __debug__ is true), 1 (asserts are removed, __debug__ is false) or 2 (docstrings are removed too).

This function raises SyntaxError if the compiled source is invalid, and ValueError if the source contains null bytes.

If you want to parse Python code into its AST representation,see ast.parse().

Note: When compiling a string with multi-line code in 'single' or 'eval' mode, input must be terminated by at least one newline character. This is to facilitate detection of incomplete and complete statements in the code
module.

Changed in version 3.2: Allowed use of Windows and Mac newlines. Also input in 'exec' mode does not have to end in a newline anymore. Added the optimize parameter.

Changed in version 3.5: Previously, TypeError was raised when null bytes were encountered in source.

說明:

  1. 將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執(zhí)行或者eval()進行求值。

  2. 參數(shù)source:字符串或者AST(Abstract Syntax Trees)對象。即需要動態(tài)執(zhí)行的代碼段。

  3. 參數(shù) filename:代碼文件名稱,如果不是從文件讀取代碼則傳遞一些可辨認的值。當(dāng)傳入了source參數(shù)時,filename參數(shù)傳入空字符即可。

  4. 參數(shù)model:指定編譯代碼的種類,可以指定為 ‘exec’,’eval’,’single’。當(dāng)source中包含流程語句時,model應(yīng)指定為‘exec’;當(dāng)source中只包含一個簡單的求值表達式,model應(yīng)指定為‘eval’;當(dāng)source中包含了交互式命令語句,model應(yīng)指定為'single'。

>>> #流程語句使用exec
>>> code1 = 'for i in range(0,10): print (i)'
>>> compile1 = compile(code1,'','exec')
>>> exec (compile1)
0123456789

>>> #簡單求值表達式用eval
>>> code2 = '1 + 2 + 3 + 4'
>>> compile2 = compile(code2,'','eval')
>>> eval(compile2)
10

>>> #交互語句用single
>>> code3 = 'name = input("please input your name:")'
>>> compile3 = compile(code3,'','single')
>>> name #執(zhí)行前name變量不存在
Traceback (most recent call last): 
  File "<pyshell#29>", line 1, in <module> 
    name
NameError: name 'name' is not defined
>>> exec(compile3) #執(zhí)行時顯示交互命令,提示輸入
please input your name:'pythoner'
>>> name #執(zhí)行后name變量有值
"'pythoner'"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容