为了直接回答你的问题,电子数据确实只有 评价<>/em>的代码才产生结果。 例如:
>>> eval( 1 + 1 )
2
然而,如果你只是想执行守则,可能的话,要执行几条法典,那么你就希望通过在呼唤者名下执行。
>>> exec("x = 5 + 5")
>>> print x
10
请注意,只有可信赖的法典才能通过。 另见执行档案的外来文件。
Having said all that, I agree with other posters that you should find a way to problematically do what you want to do instead of storing arbitrary code. You could, for example, do something like this:
def myMailCommand(...):
...
def myOtherCommand(...):
...
available_commands = { mail : myMailCommand,
other : myOtherCommand}
to_execute = [( mail , (arg1, arg2, arg3)),
( other , (arg1, arg2))]
for cmd, args in to_execute:
available_commands[cmd](*args)
在上述假体编码中,我确定了两种方法。 之后,我有一套用于指挥的字典绘图行动。 然后,我通过一个行动和论点数据结构,并相应地提出适当的论点。 你们有这个想法。