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

Maric Michaud maric at aristote.info
Wed May 24 18:06:16 EDT 2006


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]:                                                     



-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list