Python in eval, exec, execfile, and compile (reprint)

Source: Internet
Author: User

This log is reproduced, the original link: http://skandgjxa.blog.163.com/blog/static/1415298201010262403483/

eval (str [, Globals [, locals]]) function treats the string str as a valid Python table

To evaluate the value and return 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         'a=100'          >>> a                  >>> 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 both

The optional dictionary parameter is the global namespace and local namespace for code execution. For example:

To switch lines:

    1Globals = {'x':7,    2            'y':Ten,    3            'Birds': ['Parrot','Swallow','Albatross']    4           }    5Locals = { }    6     7# Use the top dictionary as the global and local namespace8A = eval ("3*x + 4*y", Globals, locals)9Exec"For b in Birds:print b" inchGlobals, locals # Note the syntax hereTenExecFile ("foo.py", globals, locals)

If you omit one or two namespace parameters, the current global and local namespaces are used. If a function is nested inside a nested function or lambda anonymous function

, when the exec or execfile () function is used in the body of a function, a SyntaxError exception is thrown because of the pull to the nested scope.

Note that the use of the EXEC statement in the example is not the same as Eval (), execfile (). EXEC is a statement (like print or while), and Eval () and execfile () are internal

Building functions.

This form of EXEC (STR) is also accepted, but it does not return a value. --weizhong


When a string is executed by Exec,eval (), or execfile (), the interpreter compiles them into byte code before executing. This process is time consuming, so if you need

It is best to pre-compile the code many times for a piece of code, so that you do not need to compile the code every time, which 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 text that defines the string variable.

, the kind parameter specifies the type of code being compiled-' single ' refers to the individual statement, ' exec ' refers to multiple statements, ' eval ' refers to an expression. The Cmpile () function returns a generation

Object, which of course can also be passed to the Eval () function and the EXEC statement to execute, for example:

    1str ="For i in range (0,10): Print i"    2c = Compile (str,"','exec') # Compile as a byte code object3exec C # execution4     5STR2 ="3*x + 4*y"    6C2 = Compile (str2,"','Eval') # compiled as an expression7result = eval (C2) # execution

Python in eval, exec, execfile, and compile (reprint)

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.