閱讀 stackoverflow 上的問題: What's the difference between eval, exec, and compile in Python?
做一下小的總結(jié):
eval() 接受 expression, 并返回其結(jié)果
exec() 接受statements, 返回值為
None
compile(): Compile the source into a code or AST object. Code objects can be executed by exec() or eval().
If a str/unicode/bytes
containing source code was passed to exec
, it behaves equivalently to:
exec(compile(source, '<string>', 'exec'))
and eval similarly behaves equivalent to:
eval(compile(source, '<string>', 'eval'))
參考
python documentation: Built-in Functions