时间:2021-07-01 10:21:17 帮助过:110人阅读
需注意的是,exec是一个语句,而eval()和execfile()则是内建built-in函数。
- 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
- >>>
同时,我们有时使用input输入一些数据,例如
- >>> input("请输入:")
- 请输入:1+2**3
- 9
- >>>
其实这里的input也是eval的应用,等效于
- >>> eval(raw_input("请输入:"))
- 请输入:1+2**3
- 9
- >>>