Python in eval, exec, execfile, and compile

Source: Internet
Author: User

The eval (str [, Globals [, locals]) function evaluates the string str as a valid Python expression and returns the result of the calculation.

Similarly, the EXEC statement executes the string str as valid Python code. The namespace of the code provided to exec is the same as the name space of the EXEC statement.

Finally, execfile (filename [, globals [, locals]]) function can be used to execute a file, see the following example:

>>> eval (' 3+4 ')

7

>>> exec ' a=100 '

>>> A

100

>>> execfile (R ' c:\test.py ')

hello,world!

>>>

By default, eval (), Exec,execfile () runs the code in the current namespace. The eval (), Exec, and execfile () functions can also accept one or two optional dictionary parameters as the global namespace and local namespace for code execution. For example:

1 globals = {' x ': 7,

2 ' y ': 10,

3 ' birds ': [' Parrot ', ' Swallow ', ' Albatross ']

4}

5 locals = {}

6

7 # Use the top dictionary as the global and local namespace

8 A = eval ("3*x + 4*y", globals, locals)

9 Exec "for B in Birds:print B" in Globals, locals # Note the syntax here

Ten execfile ("foo.py", globals, locals)

In Python2.4 I did not notice the use of the EXEC statement in the example and eval (), execfile () is not the same. EXEC is a statement (like print or while), and Eval () and execfile () are built-in functions. This form of EXEC (STR) is also accepted, but it does not return a value. When a string is executed by Exec,eval (), or execfile (), the interpreter compiles it into a byte code and then executes it. This process is time consuming, so if you need to perform many times on a piece of code, it's a good idea to precompile the code first. This will not need to compile the code every time, can effectively improve the execution efficiency of the program. The compile (str, filename, kind) function compiles a string into a byte code, and STR is the string that will be compiled, and filename is the file that defines the string variable, and the kind parameter specifies the type of code being compiled-' single ' refers to the individual statement, ' EXEC ' means multiple statements, ' eval ' refers to an expression. The Cmpile () function returns a code object that can, of course, be passed to the Eval () function and the EXEC statement to execute, for example:

1 str = "For I in Range (0,10): Print I "

2 c = Compile (str, ', ' exec ') # compiled into a byte code object

3 EXEC C # execution

4

5 str2 = "3*x + 4*y"

6 C2 = Compile (str2, ', ' eval ') # Compiled for expression

Python in eval, exec, execfile, and compile

Related Article

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.