Eval, exec, execfile, and compile in Python [reprint]

Source: Internet
Author: User

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

Similarly, the exec statement executes the string STR as a valid Python code. The namespace provided to the exec code is the same as the namespace of the exec statement.

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

>>> Eval ('3 + 4 ')
7
>>> Exec 'a = 100'
>>>
100
>>> Execfile (r 'C: \ test. py ')
Hello, world!
>>>
By default, the code run by eval (), exec, execfile () is in the current namespace. the eval (), exec, and execfile () functions can also use 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 above 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
10 execfile ("foo. py", globals, locals)

If you omit one or two namespaces, the current global and local namespaces are used. if a nested function or Lambda anonymous function is embedded in the function body and the exec or execfile () function is used in the function body, a syntaxerror exception is thrown because of the nested scope. (original article: If you omit one or both namespaces, the current values of the global and local namespaces are used. also, due to issues related
To nested scopes, the use of exec or execfile () inside a function body may result in a syntaxerror exception if that function also contains nested function definitions or uses the lambda operator .)

No exception was found in python2.4.
In this example, the exec statement is used differently than eval () and execfile. exec is a statement (like print or while), while eval () and execfile () are built-in functions.
Exec (STR) is also accepted, but it does not return a value.
When a string is executed by exec, Eval (), or execfile (), the interpreter first compiles it into byte code and then executes it. this process is time-consuming, so if you need to execute a code segment many times, you 'd better pre-compile the code first, so that you do not need to compile the code every time, it can effectively improve the execution efficiency of the program.
The compile (STR, filename, kind) function compiles a string into byte code. STR is the string to be compiled, and filename is the file that defines the string variable, the kind parameter specifies the type of the Code to be compiled. 'singles' indicates a single statement, 'exec 'indicates multiple statements, and 'eval' indicates an expression. the cmpile () function returns a code object, which can also be passed to the eval () function and exec statement for execution. For example:

1 StR = "for I in range (0, 10): print I"
2 c = compile (STR, '', 'exec ') # compile as a byte code object
3 exec C # Run
4
5 str2 = "3 * x + 4 * Y"
6 C2 = compile (str2, '', 'eval') # compile as an expression

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.