Possible to fake object type?

Michael 'Mickey' Lauer mickey at tm.informatik.uni-frankfurt.de
Sat Mar 2 20:37:26 EST 2002


Chris Liechti <cliechti at gmx.net> wrote:
>> This is basically the same as I have now. But the thing is:
>> __getattr__ only gets called when the attribute _doesn't_ exist,
> 
> you're right - i thought this has changed in py 2.2 but i never used 
> it that way.
> 
> maybe you could replace your classes __dict__ with an empty one so 
> that __getattr__ get called?

Aaah... this is it! Well, not exactly, but it gave me the idea to override
"by-hand" the (few) methods, which the baseclass implements but which should rather
be reflected to the "contained" class. My code now looks like

class Dockable( gtk.HandleBox ):
    "Repraesentiert eine andockbare Werkzeugleiste"

    _meth = ["set_style"]
    
    def __init__( self, Class ):
        gtk.HandleBox.__init__( self )
        
        self.__dict__["_child"] = Class()
        self.add( self._child )
        for meth in self._meth:
            self.__dict__[meth] = getattr( self._child, meth )
        
    def __getattr__( self, attr ):
        return getattr( self._child, attr )

It might not be a "totally political correct"[*] solution, but it works :)

Thanks your help.

Yours,

Mickey.

[*] which brings the question, when and how justified can a "bad design" be, if
it saves you a whole lot of code, especially for many ugly special cases, which
can't be a sign of good design, either? Opinions?






More information about the Python-list mailing list