The role of the EXEC () function in Python

Source: Internet
Author: User

The EXEC statement is used to execute a python statement stored in a string or file. For example, we can generate a string that contains Python code at run time, and then execute the statements using the EXEC statement. The following is a simple example.

>>> exec ' print ' Hello world '
Hello World

Note that the use of the EXEC statement in the example is not the same as Eval_r (), execfile (). EXEC is a statement (like print or while), whereas Eval_r () and execfile () are built-in functions.

An example of understanding the Web:

Actually I'm going to use it as the domain name ... Specifically, I want to achieve such an effect ...
There is a class called afunction.
Then run the following code:
Import APPUIFW
D=afunction ("age=10")
Appuifw.note (str (d.age). Decode ("Utf-8"))
Results are output 10.

Solution:
Class Afunction:
def __init__ (self,mystring):
exec ("self.") +mystring)

exec executes the stored string (self. AGE=10); So the result is D. Age = 10

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The role of the EXEC () function in Python

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.