non OO behaviour of file

Robin Becker robin at reportlab.com
Wed Jun 15 10:03:40 EDT 2005


I recently tried to create a line flushing version of sys.stdout using

class LineFlusherFile(file):
     def write(self,s):
         file.write(self,s)
         if '\n' in s:
             self.flush()

but it seems that an 'optimization' prevents the overriden write method from 
being used. I had thought python was more regular than it appears to be.

Is there a better way to accomplish the intention of the above than

class LineFlusherFile:
     def __init__(self,*args):
         self._f = open(*args)
     def __getattr__(self,a):
         return getattr(self._f,a)
     def write(self,s):
         self._f.write(s)
         if '\n' in s:
             self._f.flush()

I wondered if I could make a file subclass somehow fail the PyFile_Check which 
allows the optimization.
-- 
Robin Becker




More information about the Python-list mailing list