The python exec (), eval () and complie the ()

The python exec (), eval () and complie the ()

Reference blog: http: //www.cnblogs.com/yyds/p/6276746.html

https://www.cnblogs.com/yangmingxianshen/p/7810496.html

1.eval function

The role of the function:

Calculation of the value of the specified expression. That python code to be executed it can only be a single expression (eval note does not support any form of assignment), not a complex code logic.

eval(source, globals=None, locals=None, /)

Parameter Description:

source: Required parameter may be a string or an arbitrary code (codes) object instances (complie function can be created). If it is a string, it will be treated as a (use globals and locals arguments as global and local namespace) python expression analysis and interpretation.

globals: optional parameter represents the global name space (to store global variables), if provided, must be a dictionary object.

locals: an optional parameter indicating the global namespace (store local variables), if provided, may be any map object. If the parameter is ignored, then it will take the globals the same value.

If the globals and locals are ignored, they will take the eval () global namespace is called environment functions and local namespace.

return value:

If the source code is a target, and creates the object code, mode parameters complie function is 'exec', then the eval () function returns the value of None;

Otherwise, if a source is the output statement, such as Print (), the eval () returns a value of None;

Otherwise, the result is the source of the expression eval () function's return value

 


 

2.exec function

The role of the function:

Dynamic run python code. That can perform complex exec python code, rather than only as a function eval evaluates an expression.

exec(source, globals=None, locals=None, /)

source: Required parameter indicates python code needs to be specified. It must be a string or code objects. If the source is a string that will first be resolved to a set of python statement, and then execute. If the source is a code object, it is just a simple execution.

return value:

exec return value of the function is always None.

 

eval () function and the difference between the exec () function:

eval () function only Calcd single expression, whereas exec () function can be dynamically run code segments.

eval () function can return a value, and exec () function returns the value is always None.

 


expr The variable is the innermost variable, followed by extrapolation, search by variables, the innermost variable priority call from inside to outside, so z is always the same, and x, y then take the final assignment

expr The variable is the innermost variable, followed by extrapolation, search by variables, the innermost variable priority call from inside to outside, so z is always the same, and x, y then take the final assignment

3.complie function

The role of the function:

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

Parameter Description:

source: AST string or object representing python code to be compiled

filename: Specifies the compiled code file, if the file is not read the code are some of the recognizable value passed.

mode: as it must be used to identify the class represents compiled; if the source code is a sequence of statements, the designated mode = 'exec', if the source of a single-expression, the designated mode = 'eval'; if the source is by a separate interactive statements, specify = Modo 'sINGLE' . We must develop, or certainly will complain.

Examples

 

 

由上述例子可以看出,compile只是编译代码,exec和eval都可以执行single mode的代码;exec只是计算表达式,本身返回为None,因此使用exec计算code_eval虽然可以计算出结果,但是返回值为None,b = exec(code_eval)为None, 而eval只能执行单个的表达式,返回表达式的结果。

Guess you like

Origin www.cnblogs.com/arlison/p/11574110.html