我正在尝试在Python中创建一个与compile()相同的函数,但也让我得到原始字符串.为了消除歧义,我们将这两个函数称为comp()和decomp().那是,
a = comp("2 * (3 + x)", "", "eval")
eval(a, dict(x=3)) # => 12
decomp(a) # => "2 * (3 + x)"
Run Code Online (Sandbox Code Playgroud)
返回的字符串不必相同("2*(3 + x)"是可接受的),但它必须基本相同("2*x + 6"不会).
以下是我已经试过了不工作:
这是什么工作,有问题:
这是一个奇怪的问题,我最初的反应是,你可能最好做一些其他事情来完成你想要做的任何事情.但它仍然是一个有趣的问题,所以这是我对它的破解:我使原始代码源成为代码对象的未使用常量.
import types
def comp(source, *args, **kwargs):
"""Compile the source string; takes the same arguments as builtin compile().
Modifies the resulting code object so that the original source can be
recovered with decomp()."""
c = compile(source, *args, **kwargs)
return types.CodeType(c.co_argcount, c.co_nlocals, c.co_stacksize,
c.co_flags, c.co_code, c.co_consts + (source,), c.co_names,
c.co_varnames, c.co_filename, c.co_name, c.co_firstlineno,
c.co_lnotab, c.co_freevars, c.co_cellvars)
def decomp(code_object):
return code_object.co_consts[-1]
Run Code Online (Sandbox Code Playgroud)
>>> a = comp('2 * (3 + x)', '', 'eval')
>>> eval(a, dict(x=3))
12
>>> decomp(a)
'2 * (3 + x)'
Run Code Online (Sandbox Code Playgroud)