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

Source: Internet
Author: User

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

This function is used to execute a statement or function. The parameter object is the name of a string statement or compiled statement. The globals parameter is a global namespace used to specify the global namespace that can be accessed during statement execution. The locals parameter is a local namespace used to specify the namespace of the local scope that can be accessed during statement execution. Note that this function does not return any value, regardless of whether the function or statement has any return statement, such as the return or yield statement.

If the globals and locals parameters are ignored, the namespace used for the call will be used. Both parameters require a dictionary to describe the namespace.

I have learned compile, eval, and other functions before. What are the differences between them? The differences between them are as follows:

The compile function only compiles string code without any execution. However, it can compile expressions or statements.

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

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

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

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

Example:

# Exec () exec ('If True: print (100) ') exec (''' x = 200if x> 100: print (x + 200 )''')

The output is as follows:

100

400



Cai junsheng QQ: 9073204 Shenzhen

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.