我们都知道 exec 函数可以用来动态执行 python 代码:
|
1 2 3 |
>> exec('foobar = 123') >>> foobar 123 |
那么大家是否知道 exec 函数还支持两个可选参数呢(不支持通过关键字去指定参数)? :
|
1 |
exec(object[, globals[, locals]]) |
这两个参数可以用来指定执行代码时可以使用的全局变量和局部变量, 以及收集执行代码后的全局变量和局部变量。
- globals 默认为 globals()
- locals 默认为 globals 参数的值
下面将举例说明指定 globals 和 locals 时的效果。
globals
globals 是个 dict 对象,用来指定代码执行时可以使用的全局变量以及收集代码执行后的全局变量:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> age = 10 >>> exec('abc = age + 1') >>> exec('abc = age + 1', {}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'age' is not defined >>> exec('abc = age + 1', {'age': 2}) >>> >>> g = {'age': 2} >>> exec('abc = age + 1', g) >>> g['abc'], g['age'] (3, 2) |
如果只指定了 globals 参数,它的值将同样用于 locals 参数。
有一点需要注意的是: 当 globals 字典不包含 __builtins__ 这个 key 时, python 会自动加一个指向 builtins 的引用。所以如果要禁止/限制代码使用内置函数的话, 需要同时指定 __builtins__ 这个 key:
|
1 2 3 4 5 6 7 |
>>> exec('int(1)', {}) >>> exec('int(1)', {'__builtins__': {}}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'int' is not defined |
locals
locals 可以是任何 mapping 对象,用来指定代码执行时的局部变量以及收集代码执行后的局部变量:
|
1 2 3 4 5 6 7 8 9 10 |
>>> exec('abc = age + 1', {}, {'age': 2}) >>> >>> local = {} >>> exec(''' ... name = 'Tom' ... age = 13 ... ''', {}, local) >>> local {'age': 13, 'name': 'Tom'} |
本文详细介绍了Python中的exec函数,它能够动态执行代码字符串。讲解了exec函数的两个可选参数globals和locals,如何指定它们来控制代码执行时的全局和局部变量。通过示例展示了当不提供或提供不同变量字典时,代码执行的效果。强调了当globals字典不包含__builtins__时,如何影响代码对内置函数的访问。最后,讨论了locals参数如何定义局部变量并收集执行后的变量值。
1万+

被折叠的 条评论
为什么被折叠?



