Bind an instance of a base to a subclass - can this be done?

Lou Pecora pecoraREMOVE at THISanvil.nrl.navy.mil
Thu May 25 15:39:28 CEST 2006


In article <mailman.6167.1148508382.27775.python-list at python.org>,
 Maric Michaud <maric at aristote.info> wrote:

> Le Mercredi 24 Mai 2006 22:04, Diez B. Roggisch a écrit :
> > Nope, not in that way. But you might consider writing a proxy/wrapper
> > for an object. That looks like this (rouch sketch from head):
> >
> > class FileWrapper(object):
> >     def   init  (self, f):
> >        self. f = f
> >
> >     def   getattr  (self, name):
> >        return getattr(self. f, name)
> >
> >     def myreadline(self):
> >        ....
> Why use a proxy when you can just inherit from the builtin file object ?
> 
> class myFile(file) :
> 	def myreadline(self) : print 'yo'
> 
> 
> In [20]: myFile('frwiki-20060511-abstract.xml')
> Out[20]: <open file 'frwiki-20060511-abstract.xml', mode 'r' at 0xa78cc1e4>
> 
> In [21]: myFile('frwiki-20060511-abstract.xml').myreadline()
> yo
> 
> In [22]:

BINGO!  This is exactly what I want.  I didn't realize that I could open 
using file itself instead of open.  I did find this in another section 
of Python in a Nutshell thanks to your suggestion.  

Thank you.

And thanks to all who answered.

-- Lou Pecora  (my views are my own) REMOVE THIS to email me.



More information about the Python-list mailing list