Python code 模块
实例
创建交互式解释器对象:
import code
interp = code.InteractiveInterpreter()
print(isinstance(interp, code.InteractiveInterpreter))
亲自试一试 »
定义和用法
代码模块提供了实现读取-求值-打印循环(REPL)的功能。
使用它来嵌入交互式解释器,并安全地运行带有自定义局部变量/全局变量的代码字符串。
成员
| 成员 | 描述 |
|---|---|
| InteractiveConsole | 带输入缓冲的交互式控制台。 |
| InteractiveInterpreter | 用于执行代码字符串的基本解释器。 |
| compile_command() | 编译可能不完整的 Python 代码。 |
| interact() | 使用交互式控制台启动一个简单的 REPL(可选横幅/退出消息)。 |
| push() | InteractiveConsole 方法: 输入一行;如果需要更多输入,则返回 True。 |
| raw_input() | InteractiveConsole 方法: 读取一行输入(可重写以自定义输入源)。 |
| resetbuffer() | InteractiveConsole 方法: 清除当前多行输入缓冲区。 |
| runcode() | InteractiveInterpreter 方法: 执行已编译的代码对象。 |
| runsource() | InteractiveInterpreter 方法: 编译并运行源字符串。 |
| showsyntaxerror() | InteractiveInterpreter 方法: 以友好的方式打印最后一个语法(解析)错误。 |
| showtraceback() | InteractiveInterpreter 方法: 打印最后一次异常的回溯信息。 |
| write() | InteractiveInterpreter 方法: 写入输出(可重写以捕获或重定向)。 |

