Subclassing file and getting around the file.__init__ rigidity

Jan Burgy jburgy at hotmail.com
Sat Apr 3 11:41:31 EST 2004


Hi Franz,

your idea is exactly the type of stuff I was thinking of.
Unfortunately it doesn't work... Does anybody see what we're doing
wrong?

Thanks to all

Jan Burgy

"F. GEIGER" <fgeiger at datec.at> wrote in message news:<c4j66h$a0k$1 at newshispeed.ch>...
> # Untested code
> 
> class BufferedFile(file):
> 
>     def __init__(self, name):
>         if type(name) == file:
>             self.__dict__.update(file.__dict__) # Kinda cctor
>         else:
>             file.__init__(self, name)
>         self.buffer = None
> 
> Cheers
> Franz
> 
> "Jan Burgy" <jburgy at hotmail.com> schrieb im Newsbeitrag
> news:807692de.0404012244.53b476dc at posting.google.com...
> > Hi all y'all,
> >
> > Consider the class down below. I've implemented it just because I
> > needed the pushback method. Now of course the third line in __init__
> > doesn't work and I even understand why. My question is: is there any
> > way to make it work? Somebody proposed a patch for fileobject.c to
> > allow stuff like fp = file(fp1.fileno()) but it looks like it's been
> > rejected. I won't so bold as to request a change in Python. Should I
> > try to re-write this class in C? Although I know C I'm much to lazy to
> > take on the entire python API, not that it doesn't look nice.
> >
> > Thanks for your help
> >
> > Jan Burgy
> >
> > class BufferedFile(file):
> >
> >     def __init__(self, name):
> >         if type(name) == file:
> >             self = name                   # DOESN'T WORK!
> >         else:
> >             file.__init__(self, name)
> >         self.buffer = None
> >
> >     def readline(self):
> >         if self.buffer:
> >             i = self.buffer.find("\n")
> >             line, self.buffer = self.buffer[:i], self.buffer[:i+1]
> >         else:
> >             line = file.readline(self)
> >         return line
> >
> >     def pushback(self, line):
> >         self.buffer = line + self.buffer



More information about the Python-list mailing list