Python内置函数(62)——exec
英文文檔:
exec(object[,?globals[,?locals]])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).?[1]?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().NoteThe built-in functions?globals()?and?locals()?return the current global and local dictionary, respectively, which may be useful to pass around for use as the second and third argument to?exec().NoteThe default?locals?act as described for function?locals()?below: modifications to the default?locals?dictionary should not be attempted. Pass an explicit?locals?dictionary if you need to see effects of the code on?locals?after function?exec()returns. 動態執行語句塊說明:1.??exec函數和eval函數類似,也是執行動態語句,只不過eval函數只用于執行表達式求值,而exec函數主要用于執行語句塊。
第一個參數為語句字符串,globals參數和locals參數為可選參數,如果提供,globals參數必需是字典,locals參數為mapping對象。
>>> exec('print("你好")') 你好 >>> exec("print ('runoob.com')") runoob.com >>> 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 >>> >>> eval('a=1+2') #執行語句報錯 Traceback (most recent call last):File "<pyshell#0>", line 1, in <module>eval('a=1+2')File "<string>", line 1a=1+2^ SyntaxError: invalid syntax>>> exec('a=1+2') #執行語句 >>> a 3
2.能夠讀取局部和全部變量,并且能修改全局變量,這個要注意的是,他修改完全局變量的值后,只能在內部使用因為沒有 return 的功能,(eval 同理。)返回值永遠是 None,
x=10 expr="z=30;sum=x+y+z;print(sum);print(x)" y=20 exec(expr) print(x,y,z) exec(expr,{'x':1,'y':2,'z':1}) print(x,y) exec(expr,{'x':0,'y':1,'z':0},{'x':0,'y':0,'z':1}) x=0y=1
z=0
def expr():
x=0
y=0
z=1
z=30
sum=x+y+z
print(sum)
expr()
3. globals參數用來指定代碼執行時可以使用的全局變量以及收集代碼執行后的全局變量
>>> g = {'num':2} >>> type(g) <class 'dict'>>>> exec('num2 = num + 2',g)>>> g['num'] 2 >>> g['num2'] #收集了exec中定義的num2全局變量 4
4.?locals參數用來指定代碼執行時可以使用的局部變量以及收集代碼執行后的局部變量,如果該參數被忽略,那么它將會取與globals相同的值。
>>> g = {'num':2} >>> type(g) <class 'dict'> >>> l = {'num2':3} >>> type(l) <class 'dict'>>>> exec(''' num2 = 13 num3 = num + num2 ''',g,l)>>> l['num2'] #l中num2值已經改變 13
5. 為了保證代碼成功運行,globals參數字典不包含?__builtins__?這個 key 時,Python會自動添加一個key為?__builtins__?,value為builtins模塊的引用。如果確實要限制代碼不使用builtins模塊,需要在global添加一個key為__builtins__,value為{}的項即可(很少有人這么干吧)。
>>> g = {} >>> exec('a = abs(-1)',g) >>> >>> g = {'__builtins__':{}} >>> exec('a = abs(-1)',g) #不能使用內置函數了 Traceback (most recent call last):File "<pyshell#30>", line 1, in <module>exec('a = abs(-1)',g)File "<string>", line 1, in <module> NameError: name 'abs' is not defined
6. 當globals參數不提供是,Python默認使用globals()函數返回的字典去調用。
>>> num = 1 >>> exec('num2 = num + 1') >>> globals() {'__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__name__': '__main__', '__spec__': None, '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, 'num2': 2, 'num': 1} >>> >>> >>> exec('num2 = num + 1',{}) #指定了globals參數,globals中無num變量 執行失敗 Traceback (most recent call last):File "<pyshell#5>", line 1, in <module>exec('num2 = num + 1',{})File "<string>", line 1, in <module> NameError: name 'num' is not defined>>> l = locals() >>> l {'__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__name__': '__main__', '__spec__': None, '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, 'l': {...}, 'num2': 2, 'num': 1} >>> >>> exec('num3 = num + 1',{},l)#指定了globals參數,globals中無num變量,指定了locals變量,locals變量含有num變量 執行成功 >>> l {'__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__name__': '__main__', '__spec__': None, 'num3': 2, '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, 'l': {...}, 'num2': 2, 'num': 1} >>>
8.命名空間,為了不影響全局的命名空間,可以指定命名空間。如果不適用builtins模塊,需要在global添加一個key為__builtins__,value為{}的項。
a=2 scope={} exec('a=4',scope) dic=globals() print(a) print(scope['a']) print(dic.keys()) print(dic.items()) print(scope.keys()) print(scope.items() # scope包含在 dic 里面 g={} # 指定空的__builtins__內置函數 print(g) g= globals() print(g) exec('print(abs(-1))',g) print(g.keys()) # g={'__builtins__':{}} exec('print(abs(-1))',{})9. exec 里不能出現 break,continue等關鍵字,因為 exec 語句塊是獨立執行,不會和外部的代碼進行交互。
i = 5 j = 7 n = 0 while n < iprint("looping")exec('if j > 5;break')n += 1 #報錯: looping Traceback (most recent call last):File "<stdin>", line 4, in <module>File "<string>", line 3 SyntaxError: "break" outside loop?
轉載于:https://www.cnblogs.com/lincappu/p/8145355.html
總結
以上是生活随笔為你收集整理的Python内置函数(62)——exec的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaSE项目之聊天室swing版
- 下一篇: CSharp之界面美化