仔細研讀后學習了三個函數:
eval:計算字符串中的表達式
exec:執行字符串中的語句
execfile:用來執行一個文件
需注意的是,exec是一個語句,而eval()和execfile()則是內建built-in函數。
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32Type "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**39>>>
其實這里的input也是eval的應用,等效于
>>> eval(raw_input("請輸入:"))請輸入:1+2**39>>>
新聞熱點
疑難解答