Python performs the string expression function of eval exec execfile

  • 2020-04-02 13:55:19
  • OfStack

After careful study, I learned three functions:
Eval: evaluates an expression in a string
Exec: executes the statement in the string
Execfile: to execute a file

Note that exec is a statement, while eval() and execfile() are built-in buil-in functions.


Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> print eval("x+1")
2
>>> exec "print 'http://blog.leniy.org/python-eval-exec-execfile.html'"
http://blog.leniy.org/python-eval-exec-execfile.html
>>> 

At the same time, we sometimes use input to input some data, for example


>>> input(" Please enter the :")
 Please enter the :1+2**3
9
>>> 

In fact, this input is also an application of eval, which is equivalent to


>>> eval(raw_input(" Please enter the :"))
 Please enter the :1+2**3
9
>>> 

Related articles: