python exec函数_Python 内建函数 - exec(object[, globals[, locals]])

Manual

直译

实例

拓展阅读

ManualThis 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().

Note The 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().

Note The 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.

直译该函数支持Python代码动态执行,object必须是字符串或代码对象。如果是字符串,字符串会被解析为一套Python语句,然后被执行(除非发生语法错误)。如果是代码对象,它将会被直接执行。在所有情况下,预期执行的代码会作为有效的文件输入。要知道返回值和产出语句可能不会用于函数定义之外,即便将代码上下文传入exec()函数,返回值是None。

在所有情形下,如果缺省可选参数,代码在当前域执行;如果只提供globals,那么它必须是字典,它将会用于全局和局部变量;如果全部给定,它们用于各自的全局和局部变量,提供的locals可以是任意映射对象。记住在模块层,globals和locals是同样的字典。如果exec获取两个单独的对象作为globals和locals,那么代码会视作嵌入到类定义中来执行。

如果全局字典不含__builtins__键,那么会将内建模块的字典引用插入到该键下。这种方式下,在传入exec()前,你可以通过将自己的__builtins__字典插入到全局变量,来控制用什么builtins来执行代码。

注意:内建函数globals和locals返回各自当前的全局和局部字典,这可能对exec()传入的第二和第三个参数有帮助。

注意:对locals()的默认locals作用描述如下:不应尝试修改默认字典。在exec()返回后,如果要看代码在局部环境的效果,你需要传递一个详细的局部字典。

实例>>> source = "print('Hello world!')"

>>> c_source = compile(source, '', 'exec')

>>> exec(c_source)

Hello world!

# 'exec'模式多行

>>> x = "i = input('Number you want to show: ')\nprint(i)"

>>> c_x = compile(x, '', 'exec')

>>> exec(c_x)

Number you want to show: 12

12

拓展阅读compile()

eval()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值