여기에 코드가 있습니다. _getTestObj()에서 데모 인스턴스를 만들 때 exec에 의해 정의 된 'demo'클래스가 작동하지 않습니다.python exec 정의 클래스가 작동하지 않습니다.
파일 이름 : test.py
class runOneIni():
def _getTestObj(self):
demo(self.tcName,secSetup,doc)
def start():
#implicit define 'demo' class by exec is not working, get error in runOneIni->_getTestObj, Error is :
# NameError: name 'demo' is not defined
a='global demo'
exec(a)
str="class demo(tInvokeMethod.tInvokeMethod): pass'
exec(str)
#Explict define demo class is working
#global demo
#class demo(tInvokeMethod.tInvokeMethod): pass
if __name__ == '__main__':
start()
내가하고 싶은 것은 동적 생성 클래스입니다.이 예제에서는 'demo'라는 클래스이지만 클래스 이름은 고정되어 있지 않습니다. 가변적입니다. 그래서 제 생각은 문자열을 사용하여 클래스 문을 생성하고, exec 문자열을 생성하는 것입니다. – brike
글쎄, 그럼, 동적으로 클래스를 만들면,'exec'가 거의 필요 없다! – kindall
'동적으로 클래스를 생성하는 방법'? – brike