String printing behavior?

Michael Hudson mwh21 at cam.ac.uk
Wed Mar 1 19:31:52 EST 2000


ted.drain at jpl.nasa.gov (Ted Drain) writes:

> Hi all,
> I've got a question about the string printing behavior.  If I define a 
> functions as:
> 
> >>> def foo():
> ...    return "line1\nline2"
> 
> >>> foo()
> 'line1\013line2'
> 
> >>> print foo()
> line1
> line2
> 
> >>>
> 
> It seems to me that the default printing behavior for strings should match
> behavior of the print routine.  I realize that some people may want to
> see embedded control codes, but I would advocate a seperate method for
> printing raw byte sequences.  
> 
> We are using the python interactive prompt as a pseudo-matlab like user 
> interface and the current printing behavior is very confusing to users.
> It also means that functions that return text (like help routines)
> must print the string rather than returning it.  Returning the string
> is much more flexible because it allows the string to be captured 
> easily and redirected.
> 
> Any thoughts?

Try this:

print "\016hello\017 back to normal"

in an xterm (and maybe in other environments, I don't know).  The
point being that sending raw characters to the terminal can well and
truly mess it up.

I agree that it can be a pain though.  You do know about code.py in
the standard libary?  I'd bet you could subclass InteractiveConsole to
do what you want.

No, on inspection, maybe that would take some work.  You could do it
with bytecodehacks - plug - just replace the PRINT_EXPR with a
PRINT_ITEM, or if you're supplying the interpreter anyway, patch it.
It would be a simple change to the code for the PRINT_EXPR opcode.

"print "\033]50;lucidasanstypewriter-12\007"-ly y'rs
Michael

-- 
very few people approach me in real life and insist on proving they are
drooling idiots.                         -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list