Python executable object--exec, eval, compile

Source: Internet
Author: User

Python provides a description of the built-in functions for calling executable objects, involving exec, Eval, and compile three functions. The EXEC statement is used to execute a python statement stored in a code object, a string, a file, and the eval statement is used to calculate a valid python expression stored in a code object or string, while the compile statement provides a byte-encoded precompilation.

Of course, it is important to note that the use of exec and Eval must pay attention to security issues, especially in a network environment, where others may be given the opportunity to execute illegal statements.

1.exec

Format:exec obj

The Obj object can be a string (such as a single statement, statement block), a file object, or a code object that has been precompiled by compile.

Here's an example:

examples of exec use of Python executable objectsPython
12345678910111213 # Single Statement string>>> exec "print ' pythoner.com '" Pythoner. COM # Multi-line statement string>>> Exec "" For i in range (5): ... print "ITER time:%d"% i... """iter time : 0 iter time : 1 iter time : 2 iter time : 3 iter time : 4

Examples of code objects are explained in part 3rd.

2.eval

Format:eval( obj[, globals=globals(), locals=locals()] )

Obj can be a string object or a code object that has been compiled by compile. Globals and locals are optional, representing objects in the global and local namespaces, where globals must be a dictionary, and locals is an arbitrary mapping object.

The following examples still illustrate:

eval of Python executable objectPython
123 >>> x = 7 >>> eval( ' 3 * x ' ) +

3.compile

Format:compile( str, file, type )

The compile statement is from type types (including ' eval ': Use with eval, ' single ': With the use of exec for a singleton statement, ' EXEC ': With the use of EXEC for multiple statements) to create the statements inside STR into code objects. File is where the code is stored, usually ".

The purpose of the compile statement is to provide a one-time bytecode compilation without having to recompile it in subsequent calls.

It is also important to note that the compile used in compile and regular expressions here are not the same, albeit for the same purpose.

Here are the corresponding examples:

compile of Python executable objectsPython

12345678910111213141516171819202122 >>> eval_code = compile( ' 1+2 ', ' ', ' Eval ') >>> eval_code <code  Object <module > at  0142abf0,  File  ",  line 1 >>> eval(eval_code) 3>>> single_code = compile( ' print ' pythoner.com "', ' , ' single ' ) >>> single_code <code Object <module> at 01c68848 , File "", line 1> >>> exec(single_code) Pythoner. COM >>> exec_code = compile("" "For I in range (5): /c16> ... print "ITER time:%d"% i "" ","' , ' exec ' ) >>> exec_code <code  Object <module > at  01c68968,  File  ",  line 1 >>> exec(exec_code) iter time : 0 iter time : 1 iter time : 2 iter time : 3 iter time : 4

Python executable object--exec, eval, compile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.