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

Diez B. Roggisch deets at nospam.web.de
Thu May 25 03:21:45 CEST 2006


Maric Michaud schrieb:
> 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 ?

To be honest - in my perception file is a function. Which it isn't, but 
I see it that way :)

You are of course right, and have the better solution (unless the OP is 
not in control of file creation).

Diez



More information about the Python-list mailing list