当子类化 code.InteractiveInterpreter 时,我似乎无法按照文档中的预期运行 write() 方法。
import code
class PythonInterpreter(code.InteractiveInterpreter):
def __init__(self, localVars):
self.runResult = ''
print 'init called'
code.InteractiveInterpreter.__init__(self, localVars)
def write(self, data):
print 'write called'
self.runResult = data
test = 'Hello'
interpreter = PythonInterpreter({'test':test})
interpreter.runcode('print test')
print 'Result:' + interpreter.runResult
预期输出:
init called
write called
Result: Hello
实际输出:
init called
Hello <- shouldn't print
Result:
有什么想法吗?