overriding "print"?
Steven D. Majewski
sdm7g at virginia.edu
Thu Aug 3 18:12:52 EDT 2000
On Thu, 3 Aug 2000, Jesus Cea Avion wrote:
> Can "print" be overriden?
>
> I want to do a dual console/Tkinter application, redirecting "print"
> output to a TK window. I can't rewrite the "print" commands because the
> original application (console only) is not under my control.
>
'print' is a statement, not a function, so you can't really
change or overwrite what it does, but you don't have to.
'print' writes on sys.stdout, so what you want to do is replace
sys.stdout with a filelike object that writes to your TK window.
You can demonstrate this with the StringIO module:
>>> from StringIO import StringIO
>>> import sys
>>> buf = StringIO()
>>> sys.stdout = buf
>>> # [ type a bunch of Python commands and don't see the output ... ]
>>> sys.stdout = sys.__stdout__
>>> print buf.getvalue() # and see all of the missing print output
-- Steve M.
More information about the Python-list
mailing list