Python standard library: Built-in function exec (object[, globals[, locals])

Source: Internet
Author: User

This function executes a statement or function. The argument object is a string of statements or an object name for a compiled statement. The parameter globals is a global namespace that specifies the global namespace that can be accessed when the statement is executed; the parameter locals is a local namespace. Used to specify the namespace of the local scope that can be accessed when the statement is executed. Note that this function does not return any values, regardless of whether the function or statement has any return value statements than the return or yield statement.

If the arguments globals and locals are ignored, the namespace at which the call is made is used. Both of these parameters require a dictionary form to describe the namespace.

What is the difference between compile,eval and other functions that have been learned in the front? It is easy to think of the differences as follows:

The compile function compiles only string code without any execution, but it compiles an expression or statement.

The Eval function executes only the expression string code without executing the statement code.

x = eval ('%d + 6 '% x)

The EXEC function executes only the statement code without executing the expression code because it does not have any return value.

EXEC (' If True:print (6) ')

Example:

#exec () EXEC (' if True:print ') exec ("x = 200if x > 100:print (x + 200)")

The resulting output is as follows:

100

400



Cai Junsheng qq:9073204 Shenzhen

Python standard library: Built-in function exec (object[, globals[, locals])

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.