str = '''
flag = True
for i in range(0,3):
print i
if flag:
print "ok"
'''
c = compile(str,'','exec')
exec c
得到如下结果
0
1
2
ok
本文展示了一段Python代码示例,其中包括使用for循环打印0到2的数字,并在特定条件下打印字符串ok。该代码还展示了如何使用Python的compile函数来编译字符串形式的代码并执行。
str = '''
flag = True
for i in range(0,3):
print i
if flag:
print "ok"
'''
c = compile(str,'','exec')
exec c
得到如下结果
0
1
2
ok
1271

被折叠的 条评论
为什么被折叠?