3.22 code -- Interpreter base classes 编辑
3.22 code -- Interpreter base classes
The code
module provides facilities to implement read-eval-print loops in Python. Two classes and convenience functions are included which can be used to build applications which provide an interactive interpreter prompt.
- class InteractiveInterpreter([locals])
- This class deals with parsing and interpreter state (the user's namespace); it does not deal with input buffering or prompting or input file naming (the filename is always passed in explicitly). The optional locals argument specifies the dictionary in which code will be executed; it defaults to a newly created dictionary with key
'__name__'
set to'__console__'
and key'__doc__'
set toNone
.
- class InteractiveConsole([locals[, filename]])
- Closely emulate the behavior of the interactive Python interpreter. This class builds on InteractiveInterpreter and adds prompting using the familiar
sys.ps1
andsys.ps2
, and input buffering.
- interact([banner[, readfunc[, local]]])
- Convenience function to run a read-eval-print loop. This creates a new instance of InteractiveConsole and sets readfunc to be used as the raw_input() method, if provided. If local is provided, it is passed to the InteractiveConsole constructor for use as the default namespace for the interpreter loop. The interact() method of the instance is then run with banner passed as the banner to use, if provided. The console object is discarded after use.
- compile_command(source[, filename[, symbol]])
- This function is useful for programs that want to emulate Python's interpreter main loop (a.k.a. the read-eval-print loop). The tricky part is to determine when the user has entered an incomplete command that can be completed by entering more text (as opposed to a complete command or a syntax error). This function almost always makes the same decision as the real interpreter main loop.
source is the source string; filename is the optional filename from which source was read, defaulting to
'<input>'
; and symbol is the optional grammar start symbol, which should be either'single'
(the default) or'eval'
.Returns a code object (the same as
compile(source, filename, symbol)
) if the command is complete and valid;None
if the command is incomplete; raises SyntaxError if the command is complete and contains a syntax error, or raises OverflowError or ValueError if the command cotains an invalid literal.
Subsections
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论