今天的 Python學(xué)習(xí)教程想跟大家說(shuō)一下exec執(zhí)行函數(shù)
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到防城港網(wǎng)站設(shè)計(jì)與防城港網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋防城港地區(qū)。
exec 函數(shù)功能:執(zhí)行儲(chǔ)存在字符串或文件中的 Python 語(yǔ)句,相比于 eval,exec可以執(zhí)行更復(fù)雜的 Python 代碼。
英文解釋
This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section “File input” in the Reference Manual). Be aware that the return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.
In all cases, if the optional parts are omitted, the code is executed in the current scope. If only globals is provided, it must be a dictionary, which will be used for both the global and the local variables. If globals and locals are given, they are used for the global and local variables, respectively. If provided, locals can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition.
If the globals dictionary does not contain a value for the key __builtins__, a reference to the dictionary of the built-in module builtins is inserted under that key. That way you can control what builtins are available to the executed code by inserting your own __builtins__ dictionary into globals before passing it to exec().
注意: 在 Python2 中exec不是函數(shù),而是一個(gè)內(nèi)置語(yǔ)句(statement),但是Python 2中有一個(gè) execfile() 函數(shù)??梢岳斫鉃?Python 3 把 exec 這個(gè) statement 和 execfile() 函數(shù)的功能夠整合到一個(gè)新的 exec() 函數(shù)中去了。
所以類似功能的函數(shù)在python2中是execfile。
適用版本
Python3.x
語(yǔ)法
以下是 exec 的語(yǔ)法:
exec(object[, globals[, locals]])
參數(shù)
object:必選參數(shù),表示需要被指定的Python代碼。它必須是字符串或code對(duì)象。如果object是一個(gè)字符串,該字符串會(huì)先被解析為一組Python語(yǔ)句,然后在執(zhí)行(除非發(fā)生語(yǔ)法錯(cuò)誤)。如果object是一個(gè)code對(duì)象,那么它只是被簡(jiǎn)單的執(zhí)行。
globals:可選參數(shù),表示全局命名空間(存放全局變量),如果被提供,則必須是一個(gè)字典對(duì)象。
locals:可選參數(shù),表示當(dāng)前局部命名空間(存放局部變量),如果被提供,可以是任何映射對(duì)象。如果該參數(shù)被忽略,那么它將會(huì)取與globals相同的值。
返回值
exec 返回值永遠(yuǎn)為 None。
實(shí)例
>>>exec 'print "Hello Python"' Hello Python # 單行語(yǔ)句字符串 >>> exec "print 'pythontab.com'" pythontab.com # 多行語(yǔ)句字符串 >>> exec """for i in range(5): ... print "iter time: %d" % i ... """ iter time: 0 iter time: 1 iter time: 2 iter time: 3 iter time: 4
文章名稱:python函數(shù)專講:exec執(zhí)行函數(shù)
文章路徑:http://www.rwnh.cn/article26/pgeccg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、靜態(tài)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、網(wǎng)站維護(hù)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)