Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

27.1code -- Interpreter base classes

Thecode module provides facilities to implementread-eval-print loops in Python. Two classes and conveniencefunctions are included which can be used to build applications whichprovide an interactive interpreter prompt.

class InteractiveInterpreter([locals])
This class deals with parsing and interpreter state (the user'snamespace); it does not deal with input buffering or prompting orinput file naming (the filename is always passed in explicitly).The optionallocals argument specifies the dictionary inwhich code will be executed; it defaults to a newly createddictionary 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 onInteractiveInterpreter and addsprompting using the familiarsys.ps1 andsys.ps2, andinput buffering.

interact([banner[, readfunc[, local]]])
Convenience function to run a read-eval-print loop. This creates anew instance ofInteractiveConsole and setsreadfuncto be used as theraw_input() method, if provided. Iflocal is provided, it is passed to theInteractiveConsole constructor for use as the defaultnamespace for the interpreter loop. Theinteract() methodof the instance is then run withbanner passed as the bannerto 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'sinterpreter main loop (a.k.a. the read-eval-print loop). The trickypart is to determine when the user has entered an incomplete commandthat can be completed by entering more text (as opposed to acomplete command or a syntax error). This functionalmost always makes the same decision as the real interpretermain loop.

source is the source string;filename is the optionalfilename from which source was read, defaulting to'<input>';andsymbol is the optional grammar start symbol, which shouldbe either'single' (the default) or'eval'.

Returns a code object (the same ascompile(source,filename,symbol)) if the command is complete andvalid;None if the command is incomplete; raisesSyntaxError if the command is complete and contains asyntax error, or raisesOverflowError orValueError if the command contains an invalid literal.



Subsections


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp