compile()python3前两个参数的意义
我试图扩大我对python的各种内置模块的理解,我遇到了compile()。 我创建了一个名为compile()python3前两个参数的意义,python,Python,我试图扩大我对python的各种内置模块的理解,我遇到了compile()。 我创建了一个名为program.py的文件,它返回print(“hello”)。我打算(从表面上看)在将来的程序中使用compile()到exec其他程序 我的问题是前两个论点的意义是什么?我知道第三个选项可以是三个选项之一:exec、eval和single 如果有人理解compile()命令,请帮助我 import program compile('','','exec',flags=0,dont_inherit=
program.py
的文件,它返回print(“hello”)
。我打算(从表面上看)在将来的程序中使用compile()
到exec
其他程序
我的问题是前两个论点的意义是什么?我知道第三个选项可以是三个选项之一:exec
、eval
和single
如果有人理解compile()
命令,请帮助我
import program
compile('','','exec',flags=0,dont_inherit=False,optimize=-1)
# compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) - are the args
第一个是要编译的Python源代码。第二个是要在回溯中显示的文件名
>>> compile('foo', 'bar', 'exec')
<code object <module> at 0x7fb586b57ab0, file "bar", line 1>
>>> exec(compile('foo', 'bar', 'exec'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "bar", line 1, in <module>
NameError: name 'foo' is not defined
编译('foo','bar','exec')
<在0x7fb586b57ab0处编码对象模块,文件“bar”,第1行>
>>>exec(编译('foo','bar','exec'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“bar”,第1行,在
NameError:未定义名称“foo”
compile('a+5','eval')