代码点读机 / Python / compile

compile Python · 内置函数

把字符串形式的源码编译成代码对象,供 exec 或 eval 执行。

怎么用

compile(源码, 文件名, 模式)

小例子

c = compile('print(1+1)', '', 'exec')
exec(c)

运行结果

2

提醒:一般不需要手动使用。

官方文档:https://docs.python.org/zh-cn/3/library/functions.html#compile

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条