仔細(xì)研讀后學(xué)習(xí)了三個(gè)函數(shù):
eval:計(jì)算字符串中的表達(dá)式
exec:執(zhí)行字符串中的語句
execfile:用來執(zhí)行一個(gè)文件
需注意的是,exec是一個(gè)語句,而eval()和execfile()則是內(nèi)建built-in函數(shù)。
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>>> 同時(shí),我們有時(shí)使用input輸入一些數(shù)據(jù),例如
>>> input("請輸入:")請輸入:1+2**39>>> 其實(shí)這里的input也是eval的應(yīng)用,等效于
>>> eval(raw_input("請輸入:"))請輸入:1+2**39>>> 新聞熱點(diǎn)
疑難解答
圖片精選