redefining stdout

Mark Limber mark.limber at dvinc.com
Fri Mar 16 11:35:48 EST 2001


Hi All,

    I appologize if this is terribly naive...

    I'm embedding Python in an application and would like a
way to redefine stdout so that it writes to my custom class.

    It seems that it would be as simple as:
-------------
File : t.py

import sys

class myPrint:
  def write(self,s):
    print >> sys.__stdout__, "Here it is: %s!" %s

  def writelines(self,lines):
    for i in lines:
      self.write(i)
------------

where, of course, the actual write method would call my
own write object.

However, when I try:

>>> import sys
>>> import t # short file names = less typing!
>>> sys.stdout = t.myPrint
>>> print "hey"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method must be called with class instance 1st
argument


Help?

Thanks,
mark




More information about the Python-list mailing list