我有这个代码:Python的EXEC()的问题
def test(variable, customCode = ""):
if variable > 1:
print(">1")
if customCode != "":
exec(customCode)
if foo == 1:
print("Success")
numb = 12
code = "if variable > 1: foo = 1"
test(numb, code)
在执行时,给出了这样的错误:
所以,后来,我在的开头加入foo = 0
代码,并得到这个输出:
现在,显然,它也应该是输出Success
,但它不是。
这是什么问题?
使用Python 3
代码执行得很好,使用'> 1'然后换行符和'Success'。 –
@ l'L'l,你使用python3吗? –
@PadraicCunningham,我尝试了'2.7.6'和'3.4.3'(在Python3中它失败了),对不起,我没有提到。你的回答让我好奇 - 是否会被视为一种范围界定? –