Pretty-printing Python interactive output?

Dan L. Pierson dan at control.com
Thu Sep 9 10:47:48 EDT 1999


"Fred L. Drake, Jr." <fdrake at acm.org> wrote:
>   I think Python should define the interactive loop entirely in Python 
> to make it easier to do just this sort of thing.  Once initialization
> is complete, if sys.interactive_loop is defined, use that as the
> InteractiveConsole object, otherwise create one and use it.  That
> would allow easy extension of various aspects of the loop within the
> $PYTHONSTARTUP file.

I agree.  This rapidly leads to thinking of the interactive loop in
terms of a Lisp read-eval-print loop (a.k.a. rep loop).  Once you get
this far it's worth looking at the support conventions that some Lisps
developed for the rep loop using well known global variables.  For
example, in Common Lisp:

	*print-pretty*	pretty print all output
	*print-length*	how many elements of a list/array to print
	*print-level*   how many levels to recurse when (pretty) printing
	*print-array*	print contents or type info about arrays
	+ ++ +++		previous three forms read by loop
	-				form currently being read by loop
	* ** ***		previous three first results printed by loop
	/ // ///		previous three lists of results printed by loop

I'm not suggesting adopting these exactly!  However this does give a
good idea on what another community that worked with this type of
facility for many years found useful.

Another useful feature of many Lisp rep loops is a simple reserved
syntax for giving commands directly to the rep loop evaluator.  In
Common Lisp these typically start with ':', because any symbol
beginning with : is a keyword and always evaluates to itself (i.e.
it's automagically useless to type it to the loop anyway :-)).  While
the exact syntax magic wouldn't be useful to Python, the idea of a
predefined, use-extensible command syntax might be...

Dan Pierson, Control Technology Corporation
dan at control.com




More information about the Python-list mailing list