什麼是動態創建Python對象實例的最佳方式,當你擁有的是將Python類保存爲字符串?將類實現存儲在字符串中時創建Python對象的最佳方法是什麼?
對於背景,我正在Google應用程序引擎環境中工作,我希望能夠從類的字符串版本動態加載類。
problem = 「1,2,3,4,5」
solvertext1 = 「」」class solver:
def solve(self, problemstring):
return len(problemstring) 「」」
solvertext2 = 「」」class solver:
def solve(self, problemstring):
return problemstring[0] 「」」
solver = #The solution code here (solvertext1)
answer = solver.solve(problem) #answer should equal 9
solver = #The solution code here (solvertext2)
answer = solver.solve(problem) # answer should equal 1
這些都是您使用 – 2009-08-21 07:52:01
爲什麼不直接從文件導入類一些奇怪的報價? – 2009-08-21 10:38:23