看起来你不能在具有子功能的函数中使用exec ...
任何人都知道为什么这个Python代码不起作用?我在test2的exec上遇到错误.另外,我知道exec的风格并不好,但相信我,我正在使用exec是出于正当的理由.否则我不会用它.
#!/usr/bin/env python
#
def test1():
exec('print "hi from test1"')
test1()
def test2():
"""Test with a subfunction."""
exec('print "hi from test2"')
def subfunction():
return True
test2()
Run Code Online (Sandbox Code Playgroud)
编辑:我把bug缩小到在子功能中有一个功能.它与raise关键字无关.