Python的内置函数exec()可以从compile()中接受一个输入,并且执行,如:
我们创建一个文件test.py,输入以下内容:
x=0
print ‘x is currently:’, x
while x<5:
x+=1
print ‘incrementing x to:’,x
在python交互命令行中:
f=open(‘test.py’)
exec f
此时会正常打印
但是如果尝试再次打印,
exec f
就不会在有打印
Python的内置函数exec()可以从compile()中接受一个输入,并且执行,如:
我们创建一个文件test.py,输入以下内容:
x=0
print ‘x is currently:’, x
while x<5:
x+=1
print ‘incrementing x to:’,x
在python交互命令行中:
f=open(‘test.py’)
exec f
exec f
就不会在有打印