标签:var rem oba ack most sse nts 局部变量 数字
英文文档:
exec
(object[, globals[, locals]])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
.__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()
.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()
.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.>>> eval(‘a=1+2‘) #执行语句报错 Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> eval(‘a=1+2‘) File "<string>", line 1 a=1+2 ^ SyntaxError: invalid syntax >>> exec(‘a=1+2‘) #执行语句 >>> a 3
2. 第一个参数为语句字符串,globals参数和locals参数为可选参数,如果提供,globals参数必需是字典,locals参数为mapping对象。
3. globals参数用来指定代码执行时可以使用的全局变量以及收集代码执行后的全局变量
>>> g = {‘num‘:2} >>> type(g) <class ‘dict‘> >>> exec(‘num2 = num + 2‘,g) >>> g[‘num‘] 2 >>> g[‘num2‘] #收集了exec中定义的num2全局变量 4
4. locals参数用来指定代码执行时可以使用的局部变量以及收集代码执行后的局部变量
>>> 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()函数返回的字典去调用。当locals参数不提供时,默认使用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} >>>
标签:var rem oba ack most sse nts 局部变量 数字
原文地址:http://www.cnblogs.com/sesshoumaru/p/5998523.html